Skip to content

How to initialize a server and add tools? #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
huhu415 opened this issue Apr 12, 2025 · 2 comments
Open

How to initialize a server and add tools? #71

huhu415 opened this issue Apr 12, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@huhu415
Copy link

huhu415 commented Apr 12, 2025

I want to initialize a server, but I got an error when initializing this project. How should I fix it?

Another thing is how to add tools to the server? I didn't see any documentation about this

Image
@huhu415 huhu415 added the bug Something isn't working label Apr 12, 2025
@huhu415
Copy link
Author

huhu415 commented Apr 12, 2025

I now know how to initialize, but how do I add tools?

@stallent
Copy link

stallent commented Apr 16, 2025

Apologies for the slow reply. In case you haven't figured it out yet, here is a snippet that shows how.

let server = Server(name: "Example Server",
                        version: "1.0",
                        capabilities: Server.Capabilities(logging: nil,
                                                          prompts: nil,
                                                          resources: nil,
                                                          
                                                          tools: .init(listChanged: false)))

 await server.withMethodHandler(ListTools.self) { context in
            
    let tool1Schema: [String: Value] = ["properties":
                                                    ["param1": ["description": "An example parameter",
                                                                "type": "string"]],
                                                "$schema": "http://json-schema.org/draft-07/schema#",
                                                "additionalProperties": false,
                                                "required": ["param1"],
                                                "type": "object"]
            
     let myTool1 = Tool(name: "MyTool1",
                              description: "Something something, tool, something something",
                              inputSchema: .object(tool1Schema))
            
            
            return ListTools.Result(tools: [myTool1])
        }
    
        
await server.withMethodHandler(CallTool.self) { context in
            
            switch context.name {
                case "MyTool1":
                    return CallTool.Result(content: [.text("Tool 1 called with: \(String(describing: context.arguments))")])
                default:
                    throw MCPError.methodNotFound(context.name)
            }
            
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants