@@ -347,7 +347,7 @@ type Content struct {
347
347
Type string ` json:"type"`
348
348
Text string ` json:"text,omitempty"`
349
349
MIMEType string ` json:"mimeType,omitempty"`
350
- Data string ` json:"data,omitempty"`
350
+ Data [] byte ` json:"data,omitempty"`
351
351
Resource *Resource ` json:"resource,omitempty"`
352
352
}
353
353
@@ -359,7 +359,7 @@ type Resource struct {
359
359
URI string ` json:"uri,"`
360
360
MIMEType string ` json:"mimeType,omitempty"`
361
361
Text string ` json:"text"`
362
- Blob * string ` json:"blob"`
362
+ Blob [] byte ` json:"blob"`
363
363
}
364
364
` ` `
365
365
@@ -438,12 +438,14 @@ transport := mcp.NewCommandTransport(exec.Command("myserver"))
438
438
session , err := client.Connect (ctx, transport)
439
439
if err != nil { ... }
440
440
// Call a tool on the server.
441
- content , err := session.CallTool (ctx, " greet" , map [string ]any{" name" : " you" })
441
+ content , err := session.CallTool (ctx, &CallToolParams{
442
+ Name: " greet" ,
443
+ Arguments: map [string ]any{" name" : " you" } ,
444
+ })
442
445
...
443
446
return session.Close ()
444
447
` ` `
445
-
446
- And here's an example from the server side:
448
+ A server that can handle that client call would look like this:
447
449
448
450
` ` ` go
449
451
// Create a server with a single tool.
@@ -463,6 +465,8 @@ session until the client disconnects:
463
465
func (*Server) Run (context.Context , Transport)
464
466
` ` `
465
467
468
+
469
+
466
470
**Differences from mcp-go**: the Server APIs are very similar to mcp-go,
467
471
though the association between servers and transports is different. In
468
472
mcp-go, a single server is bound to what we would call an ` SSEHTTPHandler` ,
@@ -484,6 +488,24 @@ behavior, whereas an options struct is used here. We felt that in this case, an
484
488
options struct would be more readable, and result in cleaner package
485
489
documentation.
486
490
491
+ ### Spec Methods
492
+
493
+ As we saw above, the ` ClientSession` method for the specification's
494
+ ` CallTool` RPC takes a context and a params pointer as arguments, and returns a
495
+ result pointer and error:
496
+ ` ` ` go
497
+ func (*ClientSession) CallTool (context.Context , *CallToolParams) (*CallToolResult, error )
498
+ ` ` `
499
+ Our SDK has a method for every RPC in the spec, and their signatures all share
500
+ this form. To avoid boilerplate, we don't repeat this signature for RPCs
501
+ defined in the spec; readers may assume it when we mention a "spec method."
502
+
503
+ Why do we use params instead of the full request? JSON-RPC requests consist of a method
504
+ name and a set of parameters, and the method is already encoded in the Go method name.
505
+ Technically, the MCP spec could add a field to a request while preserving backward
506
+ compatibility, which would break the Go SDK's compatibility. But in the unlikely event
507
+ that were to happen, we would add that field to the Params struct.
508
+
487
509
### Middleware
488
510
489
511
We provide a mechanism to add MCP-level middleware, which runs after the
@@ -589,8 +611,8 @@ Both `ClientSession` and `ServerSession` expose a `Ping` method to call "ping"
589
611
on their peer.
590
612
591
613
` ` ` go
592
- func (c *ClientSession) Ping (ctx context.Context ) error
593
- func (c *ServerSession) Ping (ctx context.Context ) error
614
+ func (c *ClientSession) Ping (ctx context.Context , *PingParams ) error
615
+ func (c *ServerSession) Ping (ctx context.Context , *PingParams ) error
594
616
` ` `
595
617
596
618
Additionally, client and server sessions can be configured with automatic
@@ -634,14 +656,12 @@ func (*Client) AddRoots(roots ...Root)
634
656
func (*Client) RemoveRoots (uris ...string )
635
657
` ` `
636
658
637
- Servers can call ` ListRoots` to get the roots. If a server installs a
659
+ Servers can call the spec method ` ListRoots` to get the roots. If a server installs a
638
660
` RootsChangedHandler` , it will be called when the client sends a roots-changed
639
661
notification, which happens whenever the list of roots changes after a
640
662
connection has been established.
641
663
642
664
` ` ` go
643
- func (*Server) ListRoots (context.Context , *ListRootsParams) (*ListRootsResult, error )
644
-
645
665
type ServerOptions {
646
666
...
647
667
// If non-nil, called when a client sends a roots-changed notification.
@@ -652,15 +672,13 @@ type ServerOptions {
652
672
### Sampling
653
673
654
674
Clients that support sampling are created with a ` CreateMessageHandler` option
655
- for handling server calls. To perform sampling, a server calls ` CreateMessage` .
675
+ for handling server calls. To perform sampling, a server calls the spec method ` CreateMessage` .
656
676
657
677
` ` ` go
658
678
type ClientOptions struct {
659
679
...
660
680
CreateMessageHandler func (context.Context , *ClientSession, *CreateMessageParams) (*CreateMessageResult, error )
661
681
}
662
-
663
- func (*ServerSession) CreateMessage (context.Context , *CreateMessageParams) (*CreateMessageResult, error )
664
682
` ` `
665
683
666
684
## Server Features
@@ -839,12 +857,7 @@ server.AddPrompts(
839
857
server.RemovePrompts (" code_review" )
840
858
` ` `
841
859
842
- Clients can call ` ListPrompts` to list the available prompts and ` GetPrompt` to get one.
843
-
844
- ` ` ` go
845
- func (*ClientSession) ListPrompts (context.Context , *ListPromptParams) (*ListPromptsResult, error )
846
- func (*ClientSession) GetPrompt (context.Context , *GetPromptParams) (*GetPromptResult, error )
847
- ` ` `
860
+ Clients can call the spec method ` ListPrompts` to list the available prompts and the spec method ` GetPrompt` to get one.
848
861
849
862
**Differences from mcp-go**: We provide a ` NewPrompt` helper to bind a prompt
850
863
handler to a Go function using reflection to derive its arguments. We provide
@@ -942,9 +955,9 @@ A client will receive these notifications if it was created with the correspondi
942
955
` ` ` go
943
956
type ClientOptions struct {
944
957
...
945
- ToolListChangedHandler func (context.Context , *ClientConnection , *ToolListChangedParams)
946
- PromptListChangedHandler func (context.Context , *ClientConnection , *PromptListChangedParams)
947
- ResourceListChangedHandler func (context.Context , *ClientConnection , *ResourceListChangedParams)
958
+ ToolListChangedHandler func (context.Context , *ClientSession , *ToolListChangedParams)
959
+ PromptListChangedHandler func (context.Context , *ClientSession , *PromptListChangedParams)
960
+ ResourceListChangedHandler func (context.Context , *ClientSession , *ResourceListChangedParams)
948
961
}
949
962
` ` `
950
963
@@ -954,12 +967,7 @@ feature-specific handlers here.
954
967
955
968
### Completion
956
969
957
- Clients call ` Complete` to request completions.
958
-
959
- ` ` ` go
960
- func (*ClientSession) Complete (context.Context , *CompleteParams) (*CompleteResult, error )
961
- ` ` `
962
-
970
+ Clients call the spec method ` Complete` to request completions.
963
971
Servers automatically handle these requests based on their collections of
964
972
prompts and resources.
965
973
@@ -968,23 +976,44 @@ defined its server-side behavior.
968
976
969
977
### Logging
970
978
979
+ Server-to-client logging is configured with ` ServerOptions` :
980
+
981
+ ` ` ` go
982
+ type ServerOptions {
983
+ ...
984
+ // The value for the "logger" field of the notification.
985
+ LoggerName string
986
+ // Log notifications to a single ClientSession will not be
987
+ // send more frequently than this duration.
988
+ LogInterval time.Duration
989
+ }
990
+ ` ` `
991
+
971
992
ServerSessions have access to a ` slog.Logger ` that writes to the client. A call to
972
993
a log method like ` Info` is translated to a ` LoggingMessageNotification` as
973
994
follows:
974
995
975
- - An attribute with key "logger" is used to populate the "logger" field of the notification.
976
-
977
- - The remaining attributes and the message populate the "data" field with the
996
+ - The attributes and the message populate the "data" property with the
978
997
output of a ` slog.JSONHandler ` : The result is always a JSON object, with the
979
998
key "msg" for the message.
980
999
1000
+ - If the ` LoggerName` server option is set, it populates the "logger" property.
1001
+
981
1002
- The standard slog levels ` Info` , ` Debug` , ` Warn` and ` Error` map to the
982
1003
corresponding levels in the MCP spec. The other spec levels will be mapped
983
1004
to integers between the slog levels. For example, "notice" is level 2 because
984
1005
it is between "warning" (slog value 4) and "info" (slog value 0).
985
1006
The ` mcp` package defines consts for these levels. To log at the "notice"
986
1007
level, a handler would call ` session.Log (ctx, mcp.LevelNotice , " message" )` .
987
1008
1009
+ A client that wishes to receive log messages must provide a handler:
1010
+ ` ` ` go
1011
+ type ClientOptions struct {
1012
+ ...
1013
+ LogMessageHandler func (context.Context , *ClientSession, *LoggingMessageParams)
1014
+ }
1015
+ ` ` `
1016
+
988
1017
### Pagination
989
1018
990
1019
<!-- TODO: needs design -->
0 commit comments