MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / handle

Method handle

mcp/server.go:1424–1453  ·  view source on GitHub ↗

handle invokes the method described by the given JSON RPC request.

(ctx context.Context, req *jsonrpc.Request)

Source from the content-addressed store, hash-verified

1422
1423// handle invokes the method described by the given JSON RPC request.
1424func (ss *ServerSession) handle(ctx context.Context, req *jsonrpc.Request) (any, error) {
1425 ss.mu.Lock()
1426 initialized := ss.state.InitializeParams != nil
1427 ss.mu.Unlock()
1428
1429 // From the spec:
1430 // "The client SHOULD NOT send requests other than pings before the server
1431 // has responded to the initialize request."
1432 switch req.Method {
1433 case methodInitialize, methodPing, notificationInitialized:
1434 default:
1435 if !initialized {
1436 ss.server.opts.Logger.Error("method invalid during initialization", "method", req.Method)
1437 return nil, fmt.Errorf("method %q is invalid during session initialization", req.Method)
1438 }
1439 }
1440
1441 // modelcontextprotocol/go-sdk#26: handle calls asynchronously, and
1442 // notifications synchronously, except for 'initialize' which shouldn't be
1443 // asynchronous to other
1444 if req.IsCall() && req.Method != methodInitialize {
1445 jsonrpc2.Async(ctx)
1446 }
1447
1448 // For the streamable transport, we need the request ID to correlate
1449 // server->client calls and notifications to the incoming request from which
1450 // they originated. See [idContextKey] for details.
1451 ctx = context.WithValue(ctx, idContextKey{}, req.ID)
1452 return handleReceive(ctx, ss, req)
1453}
1454
1455// InitializeParams returns the InitializeParams provided during the client's
1456// initial connection.

Callers

nothing calls this directly

Calls 4

AsyncFunction · 0.92
handleReceiveFunction · 0.85
IsCallMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected