|
| 1 | +// Copyright 2025 The Go MCP SDK Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by an MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "bufio" |
| 9 | + "context" |
| 10 | + "errors" |
| 11 | + "io" |
| 12 | + "log" |
| 13 | + "os" |
| 14 | + |
| 15 | + "github.com/modelcontextprotocol/go-sdk/jsonrpc" |
| 16 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 17 | +) |
| 18 | + |
| 19 | +// IOTransport is a simplified implementation of a transport that communicates using |
| 20 | +// newline-delimited JSON over an io.Reader and io.Writer. It is similar to ioTransport |
| 21 | +// in transport.go and serves as a demonstration of how to implement a custom transport. |
| 22 | +type IOTransport struct { |
| 23 | + r *bufio.Reader |
| 24 | + w io.Writer |
| 25 | +} |
| 26 | + |
| 27 | +// NewIOTransport creates a new IOTransport with the given io.Reader and io.Writer. |
| 28 | +func NewIOTransport(r io.Reader, w io.Writer) *IOTransport { |
| 29 | + return &IOTransport{ |
| 30 | + r: bufio.NewReader(r), |
| 31 | + w: w, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// ioConn is a connection that uses newlines to delimit messages. It implements [mcp.Connection]. |
| 36 | +type ioConn struct { |
| 37 | + r *bufio.Reader |
| 38 | + w io.Writer |
| 39 | +} |
| 40 | + |
| 41 | +// Connect implements [mcp.Transport.Connect] by creating a new ioConn. |
| 42 | +func (t *IOTransport) Connect(ctx context.Context) (mcp.Connection, error) { |
| 43 | + return &ioConn{ |
| 44 | + r: t.r, |
| 45 | + w: t.w, |
| 46 | + }, nil |
| 47 | +} |
| 48 | + |
| 49 | +// Read implements [mcp.Connection.Read], assuming messages are newline-delimited JSON. |
| 50 | +func (t *ioConn) Read(context.Context) (jsonrpc.Message, error) { |
| 51 | + data, err := t.r.ReadBytes('\n') |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + |
| 56 | + return jsonrpc.DecodeMessage(data[:len(data)-1]) |
| 57 | +} |
| 58 | + |
| 59 | +// Write implements [mcp.Connection.Write], appending a newline delimiter after the message. |
| 60 | +func (t *ioConn) Write(_ context.Context, msg jsonrpc.Message) error { |
| 61 | + data, err := jsonrpc.EncodeMessage(msg) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + _, err1 := t.w.Write(data) |
| 67 | + _, err2 := t.w.Write([]byte{'\n'}) |
| 68 | + return errors.Join(err1, err2) |
| 69 | +} |
| 70 | + |
| 71 | +// Close implements [mcp.Connection.Close]. Since this is a simplified example, it is a no-op. |
| 72 | +func (t *ioConn) Close() error { |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +// SessionID implements [mcp.Connection.SessionID]. Since this is a simplified example, |
| 77 | +// it returns an empty session ID. |
| 78 | +func (t *ioConn) SessionID() string { |
| 79 | + return "" |
| 80 | +} |
| 81 | + |
| 82 | +// HiArgs is the argument type for the SayHi tool. |
| 83 | +type HiArgs struct { |
| 84 | + Name string `json:"name" mcp:"the name to say hi to"` |
| 85 | +} |
| 86 | + |
| 87 | +// SayHi is a tool handler that responds with a greeting. |
| 88 | +func SayHi(ctx context.Context, ss *mcp.ServerSession, params *mcp.CallToolParamsFor[HiArgs]) (*mcp.CallToolResultFor[struct{}], error) { |
| 89 | + return &mcp.CallToolResultFor[struct{}]{ |
| 90 | + Content: []mcp.Content{ |
| 91 | + &mcp.TextContent{Text: "Hi " + params.Arguments.Name}, |
| 92 | + }, |
| 93 | + }, nil |
| 94 | +} |
| 95 | + |
| 96 | +func main() { |
| 97 | + server := mcp.NewServer(&mcp.Implementation{Name: "greeter"}, nil) |
| 98 | + mcp.AddTool(server, &mcp.Tool{Name: "greet", Description: "say hi"}, SayHi) |
| 99 | + |
| 100 | + // Run the server with a custom IOTransport using stdio as the io.Reader and io.Writer. |
| 101 | + transport := &IOTransport{ |
| 102 | + r: bufio.NewReader(os.Stdin), |
| 103 | + w: os.Stdout, |
| 104 | + } |
| 105 | + err := server.Run(context.Background(), transport) |
| 106 | + if err != nil { |
| 107 | + log.Println("[ERROR]: Failed to run server:", err) |
| 108 | + } |
| 109 | +} |
0 commit comments