all: add examples of customizing tool schemas, and clarify documentation (#421)
Add example/server/toolschemas, which demonstrates various ways to customize tool schemas, and illustrates the differences between ToolHandler and ToolHandlerFor. Fixes #385 Fixes #386 For #368
This commit is contained in:
parent
5ccd97fafc
commit
40b6bd30f1
2 changed files with 309 additions and 100 deletions
|
|
@ -18,18 +18,6 @@ software development kit (SDK) for the Model Context Protocol (MCP).
|
|||
> changes. We aim to tag v1.0.0 in September, 2025. See
|
||||
> https://github.com/modelcontextprotocol/go-sdk/issues/328 for details.
|
||||
|
||||
## Design
|
||||
|
||||
The design doc for this SDK is at [design.md](./design/design.md), which was
|
||||
initially reviewed at
|
||||
[modelcontextprotocol/discussions/364](https://github.com/orgs/modelcontextprotocol/discussions/364).
|
||||
|
||||
Further design discussion should occur in
|
||||
[issues](https://github.com/modelcontextprotocol/go-sdk/issues) (for concrete
|
||||
proposals) or
|
||||
[discussions](https://github.com/modelcontextprotocol/go-sdk/discussions) for
|
||||
open-ended discussion. See [CONTRIBUTING.md](/CONTRIBUTING.md) for details.
|
||||
|
||||
## Package documentation
|
||||
|
||||
The SDK consists of two importable packages:
|
||||
|
|
@ -41,21 +29,39 @@ The SDK consists of two importable packages:
|
|||
- The
|
||||
[`github.com/modelcontextprotocol/go-sdk/jsonrpc`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/jsonrpc) package is for users implementing
|
||||
their own transports.
|
||||
|
||||
- The
|
||||
[`github.com/modelcontextprotocol/go-sdk/auth`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth)
|
||||
package provides some primitives for supporting oauth.
|
||||
|
||||
## Example
|
||||
## Getting started
|
||||
|
||||
In this example, an MCP client communicates with an MCP server running in a
|
||||
sidecar process:
|
||||
|
||||
%include client/client.go -
|
||||
|
||||
Here's an example of the corresponding server component, which communicates
|
||||
with its client over stdin/stdout:
|
||||
To get started creating an MCP server, create an `mcp.Server` instance, add
|
||||
features to it, and then run it over an `mcp.Transport`. For example, this
|
||||
server adds a single simple tool, and then connects it to clients over
|
||||
stdin/stdout:
|
||||
|
||||
%include server/server.go -
|
||||
|
||||
The [`examples/`](/examples/) directory contains more example clients and servers.
|
||||
To communicate with that server, we can similarly create an `mcp.Client` and
|
||||
connect it to the corresponding server, by running the server command and
|
||||
communicating over its stdin/stdout:
|
||||
|
||||
%include client/client.go -
|
||||
|
||||
The [`examples/`](/examples/) directory contains more example clients and
|
||||
servers.
|
||||
|
||||
## Design
|
||||
|
||||
The design doc for this SDK is at [design.md](./design/design.md), which was
|
||||
initially reviewed at
|
||||
[modelcontextprotocol/discussions/364](https://github.com/orgs/modelcontextprotocol/discussions/364).
|
||||
|
||||
Further design discussion should occur in
|
||||
[issues](https://github.com/modelcontextprotocol/go-sdk/issues) (for concrete
|
||||
proposals) or
|
||||
[discussions](https://github.com/modelcontextprotocol/go-sdk/discussions) for
|
||||
open-ended discussion. See [CONTRIBUTING.md](/CONTRIBUTING.md) for details.
|
||||
|
||||
## Acknowledgements
|
||||
|
||||
|
|
|
|||
|
|
@ -12,20 +12,21 @@ import (
|
|||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
)
|
||||
|
||||
type HiParams struct {
|
||||
type Input struct {
|
||||
Name string `json:"name" jsonschema:"the name of the person to greet"`
|
||||
}
|
||||
|
||||
func SayHi(ctx context.Context, req *mcp.CallToolRequest, args HiParams) (*mcp.CallToolResult, any, error) {
|
||||
return &mcp.CallToolResult{
|
||||
Content: []mcp.Content{&mcp.TextContent{Text: "Hi " + args.Name}},
|
||||
}, nil, nil
|
||||
type Output struct {
|
||||
Greeting string `json:"greeting" jsonschema:"the greeting to tell to the user"`
|
||||
}
|
||||
|
||||
func SayHi(ctx context.Context, req *mcp.CallToolRequest, input Input) (*mcp.CallToolResult, Output, error) {
|
||||
return nil, Output{Greeting: "Hi " + input.Name}, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Create a server with a single tool.
|
||||
server := mcp.NewServer(&mcp.Implementation{Name: "greeter", Version: "v1.0.0"}, nil)
|
||||
|
||||
mcp.AddTool(server, &mcp.Tool{Name: "greet", Description: "say hi"}, SayHi)
|
||||
// Run the server over stdin/stdout, until the client disconnects
|
||||
if err := server.Run(context.Background(), &mcp.StdioTransport{}); err != nil {
|
||||
|
|
|
|||
Loading…
Reference in a new issue