Skip to content

Commit 5a3fcfc

Browse files
authored
Merge branch 'modelcontextprotocol:main' into feat-devcontainer-config
2 parents 13d158a + 2b6f7b5 commit 5a3fcfc

File tree

8 files changed

+176
-203
lines changed

8 files changed

+176
-203
lines changed

examples/sse/main.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ package main
77
import (
88
"context"
99
"flag"
10+
"fmt"
1011
"log"
1112
"net/http"
13+
"os"
1214

1315
"github.com/modelcontextprotocol/go-sdk/mcp"
1416
)
1517

16-
var httpAddr = flag.String("http", "", "use SSE HTTP at this address")
18+
var (
19+
host = flag.String("host", "localhost", "host to listen on")
20+
port = flag.String("port", "8080", "port to listen on")
21+
)
1722

1823
type SayHiParams struct {
1924
Name string `json:"name"`
@@ -28,19 +33,27 @@ func SayHi(ctx context.Context, cc *mcp.ServerSession, params *mcp.CallToolParam
2833
}
2934

3035
func main() {
36+
flag.Usage = func() {
37+
fmt.Fprintf(os.Stderr, "Usage: %s [options]\n\n", os.Args[0])
38+
fmt.Fprintf(os.Stderr, "This program runs MCP servers over SSE HTTP.\n\n")
39+
fmt.Fprintf(os.Stderr, "Options:\n")
40+
flag.PrintDefaults()
41+
fmt.Fprintf(os.Stderr, "\nEndpoints:\n")
42+
fmt.Fprintf(os.Stderr, " /greeter1 - Greeter 1 service\n")
43+
fmt.Fprintf(os.Stderr, " /greeter2 - Greeter 2 service\n")
44+
os.Exit(1)
45+
}
3146
flag.Parse()
3247

33-
if httpAddr == nil || *httpAddr == "" {
34-
log.Fatal("http address not set")
35-
}
48+
addr := fmt.Sprintf("%s:%s", *host, *port)
3649

3750
server1 := mcp.NewServer(&mcp.Implementation{Name: "greeter1"}, nil)
3851
mcp.AddTool(server1, &mcp.Tool{Name: "greet1", Description: "say hi"}, SayHi)
3952

4053
server2 := mcp.NewServer(&mcp.Implementation{Name: "greeter2"}, nil)
4154
mcp.AddTool(server2, &mcp.Tool{Name: "greet2", Description: "say hello"}, SayHi)
4255

43-
log.Printf("MCP servers serving at %s", *httpAddr)
56+
log.Printf("MCP servers serving at %s", addr)
4457
handler := mcp.NewSSEHandler(func(request *http.Request) *mcp.Server {
4558
url := request.URL.Path
4659
log.Printf("Handling request for URL %s\n", url)
@@ -53,5 +66,5 @@ func main() {
5366
return nil
5467
}
5568
})
56-
log.Fatal(http.ListenAndServe(*httpAddr, handler))
69+
log.Fatal(http.ListenAndServe(addr, handler))
5770
}

internal/jsonrpc2/conn.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (c *Connection) updateInFlight(f func(*inFlightState)) {
125125
// that and avoided making any updates that would cause the state to be
126126
// non-idle.)
127127
if !s.idle() {
128-
panic("jsonrpc2_v2: updateInFlight transitioned to non-idle when already done")
128+
panic("jsonrpc2: updateInFlight transitioned to non-idle when already done")
129129
}
130130
return
131131
default:
@@ -718,7 +718,7 @@ func (c *Connection) processResult(from any, req *incomingRequest, result any, e
718718
req.cancel()
719719
c.updateInFlight(func(s *inFlightState) {
720720
if s.incoming == 0 {
721-
panic("jsonrpc2_v2: processResult called when incoming count is already zero")
721+
panic("jsonrpc2: processResult called when incoming count is already zero")
722722
}
723723
s.incoming--
724724
})

mcp/create-repo.sh

Lines changed: 0 additions & 80 deletions
This file was deleted.

mcp/server.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const DefaultPageSize = 1000
2929
// A Server is an instance of an MCP server.
3030
//
3131
// Servers expose server-side MCP features, which can serve one or more MCP
32-
// sessions by using [Server.Start] or [Server.Run].
32+
// sessions by using [Server.Run].
3333
type Server struct {
3434
// fixed at creation
3535
impl *Implementation
@@ -69,13 +69,21 @@ type ServerOptions struct {
6969
SubscribeHandler func(context.Context, *SubscribeParams) error
7070
// Function called when a client session unsubscribes from a resource.
7171
UnsubscribeHandler func(context.Context, *UnsubscribeParams) error
72+
// If true, advertises the prompts capability during initialization,
73+
// even if no prompts have been registered.
74+
HasPrompts bool
75+
// If true, advertises the resources capability during initialization,
76+
// even if no resources have been registered.
77+
HasResources bool
78+
// If true, advertises the tools capability during initialization,
79+
// even if no tools have been registered.
80+
HasTools bool
7281
}
7382

7483
// NewServer creates a new MCP server. The resulting server has no features:
7584
// add features using the various Server.AddXXX methods, and the [AddTool] function.
7685
//
77-
// The server can be connected to one or more MCP clients using [Server.Start]
78-
// or [Server.Run].
86+
// The server can be connected to one or more MCP clients using [Server.Run].
7987
//
8088
// The first argument must not be nil.
8189
//
@@ -230,16 +238,17 @@ func (s *Server) capabilities() *serverCapabilities {
230238
defer s.mu.Unlock()
231239

232240
caps := &serverCapabilities{
241+
// TODO(samthanawalla): check for completionHandler before advertising capability.
233242
Completions: &completionCapabilities{},
234243
Logging: &loggingCapabilities{},
235244
}
236-
if s.tools.len() > 0 {
245+
if s.opts.HasTools || s.tools.len() > 0 {
237246
caps.Tools = &toolCapabilities{ListChanged: true}
238247
}
239-
if s.prompts.len() > 0 {
248+
if s.opts.HasPrompts || s.prompts.len() > 0 {
240249
caps.Prompts = &promptCapabilities{ListChanged: true}
241250
}
242-
if s.resources.len() > 0 || s.resourceTemplates.len() > 0 {
251+
if s.opts.HasResources || s.resources.len() > 0 || s.resourceTemplates.len() > 0 {
243252
caps.Resources = &resourceCapabilities{ListChanged: true}
244253
if s.opts.SubscribeHandler != nil {
245254
caps.Resources.Subscribe = true
@@ -733,7 +742,7 @@ func (ss *ServerSession) handle(ctx context.Context, req *jsonrpc.Request) (any,
733742
}
734743
// For the streamable transport, we need the request ID to correlate
735744
// server->client calls and notifications to the incoming request from which
736-
// they originated. See [idContext] for details.
745+
// they originated. See [idContextKey] for details.
737746
ctx = context.WithValue(ctx, idContextKey{}, req.ID)
738747
return handleReceive(ctx, ss, req)
739748
}

mcp/server_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,22 @@ func TestServerCapabilities(t *testing.T) {
333333
Tools: &toolCapabilities{ListChanged: true},
334334
},
335335
},
336+
{
337+
name: "With initial capabilities",
338+
configureServer: func(s *Server) {},
339+
serverOpts: ServerOptions{
340+
HasPrompts: true,
341+
HasResources: true,
342+
HasTools: true,
343+
},
344+
wantCapabilities: &serverCapabilities{
345+
Completions: &completionCapabilities{},
346+
Logging: &loggingCapabilities{},
347+
Prompts: &promptCapabilities{ListChanged: true},
348+
Resources: &resourceCapabilities{ListChanged: true},
349+
Tools: &toolCapabilities{ListChanged: true},
350+
},
351+
},
336352
}
337353

338354
for _, tc := range testCases {

mcp/shared.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type MethodHandler[S Session] func(ctx context.Context, _ S, method string, para
4444
// the compiler would complain.
4545
type methodHandler any // MethodHandler[*ClientSession] | MethodHandler[*ServerSession]
4646

47-
// A Session is either a ClientSession or a ServerSession.
47+
// A Session is either a [ClientSession] or a [ServerSession].
4848
type Session interface {
4949
*ClientSession | *ServerSession
5050
// ID returns the session ID, or the empty string if there is none.
@@ -57,7 +57,7 @@ type Session interface {
5757
getConn() *jsonrpc2.Connection
5858
}
5959

60-
// Middleware is a function from MethodHandlers to MethodHandlers.
60+
// Middleware is a function from [MethodHandler] to [MethodHandler].
6161
type Middleware[S Session] func(MethodHandler[S]) MethodHandler[S]
6262

6363
// addMiddleware wraps the handler in the middleware functions.
@@ -203,7 +203,7 @@ func serverMethod[P Params, R Result](
203203
}
204204
}
205205

206-
// clientMethod is glue for creating a typedMethodHandler from a method on Server.
206+
// clientMethod is glue for creating a typedMethodHandler from a method on Client.
207207
func clientMethod[P Params, R Result](
208208
f func(*Client, context.Context, *ClientSession, P) (R, error),
209209
) typedMethodHandler[*ClientSession, P, R] {

0 commit comments

Comments
 (0)