Skip to content

Commit 930c96a

Browse files
committed
mcp: refactor streamable serving and support JSON responses
WIP DO NOT REVIEW DO NOT SUBMIT Fixes #211
1 parent 5bd02a3 commit 930c96a

File tree

7 files changed

+450
-277
lines changed

7 files changed

+450
-277
lines changed

mcp/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ func (c *Client) Connect(ctx context.Context, t Transport) (cs *ClientSession, e
134134
return nil, unsupportedProtocolVersionError{res.ProtocolVersion}
135135
}
136136
cs.initializeResult = res
137-
if hc, ok := cs.mcpConn.(httpConnection); ok {
138-
hc.setProtocolVersion(res.ProtocolVersion)
137+
if hc, ok := cs.mcpConn.(clientConnection); ok {
138+
hc.initialized(res)
139139
}
140140
if err := handleNotify(ctx, cs, notificationInitialized, &InitializedParams{}); err != nil {
141141
_ = cs.Close()
@@ -250,7 +250,7 @@ func (c *Client) listRoots(_ context.Context, _ *ClientSession, _ *ListRootsPara
250250
func (c *Client) createMessage(ctx context.Context, cs *ClientSession, params *CreateMessageParams) (*CreateMessageResult, error) {
251251
if c.opts.CreateMessageHandler == nil {
252252
// TODO: wrap or annotate this error? Pick a standard code?
253-
return nil, &jsonrpc2.WireError{Code: CodeUnsupportedMethod, Message: "client does not support CreateMessage"}
253+
return nil, jsonrpc2.NewError(codeUnsupportedMethod, "client does not support CreateMessage")
254254
}
255255
return c.opts.CreateMessageHandler(ctx, cs, params)
256256
}

mcp/mcp_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ func TestEndToEnd(t *testing.T) {
281281
} {
282282
rres, err := cs.ReadResource(ctx, &ReadResourceParams{URI: tt.uri})
283283
if err != nil {
284-
if code := errorCode(err); code == CodeResourceNotFound {
284+
if code := errorCode(err); code == codeResourceNotFound {
285285
if tt.mimeType != "" {
286286
t.Errorf("%s: not found but expected it to be", tt.uri)
287287
}

mcp/resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type ResourceHandler func(context.Context, *ServerSession, *ReadResourceParams)
4141
// not be found.
4242
func ResourceNotFoundError(uri string) error {
4343
return &jsonrpc2.WireError{
44-
Code: CodeResourceNotFound,
44+
Code: codeResourceNotFound,
4545
Message: "Resource not found",
4646
Data: json.RawMessage(fmt.Sprintf(`{"uri":%q}`, uri)),
4747
}

mcp/shared.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,9 @@ func sessionMethod[S Session, P Params, R Result](f func(S, context.Context, P)
275275

276276
// Error codes
277277
const (
278-
CodeResourceNotFound = -32002
278+
codeResourceNotFound = -32002
279279
// The error code if the method exists and was called properly, but the peer does not support it.
280-
CodeUnsupportedMethod = -31001
280+
codeUnsupportedMethod = -31001
281281
)
282282

283283
func callNotificationHandler[S Session, P any](ctx context.Context, h func(context.Context, S, *P), sess S, params *P) (Result, error) {

0 commit comments

Comments
 (0)