mcp: change tool handler design (#325)
Change the design of tool handlers by removing genericity from the common path. - `ToolHandler` gets the args as a `json.RawMessage`. Using Server.AddTool does no unmarshaling or schema validation. - `ToolHandlerFor`, installed with `AddTool`, are the only generic pieces. TODO: uncomment and fix some tests
This commit is contained in:
parent
79f063bc98
commit
32cf71de3a
1 changed files with 789 additions and 966 deletions
|
|
@ -33,11 +33,11 @@ type hiParams struct {
|
|||
// TODO(jba): after schemas are stateless (WIP), this can be a variable.
|
||||
func greetTool() *Tool { return &Tool{Name: "greet", Description: "say hi"} }
|
||||
|
||||
func sayHi(ctx context.Context, req *ServerRequest[*CallToolParamsFor[hiParams]]) (*CallToolResultFor[any], error) {
|
||||
func sayHi(ctx context.Context, req *ServerRequest[*CallToolParams], args hiParams) (*CallToolResult, any, error) {
|
||||
if err := req.Session.Ping(ctx, nil); err != nil {
|
||||
return nil, fmt.Errorf("ping failed: %v", err)
|
||||
return nil, nil, fmt.Errorf("ping failed: %v", err)
|
||||
}
|
||||
return &CallToolResultFor[any]{Content: []Content{&TextContent{Text: "hi " + req.Params.Arguments.Name}}}, nil
|
||||
return &CallToolResult{Content: []Content{&TextContent{Text: "hi " + args.Name}}}, nil, nil
|
||||
}
|
||||
|
||||
var codeReviewPrompt = &Prompt{
|
||||
|
|
@ -97,9 +97,9 @@ func TestEndToEnd(t *testing.T) {
|
|||
Name: "greet",
|
||||
Description: "say hi",
|
||||
}, sayHi)
|
||||
s.AddTool(&Tool{Name: "fail", InputSchema: &jsonschema.Schema{}},
|
||||
func(context.Context, *ServerRequest[*CallToolParamsFor[map[string]any]]) (*CallToolResult, error) {
|
||||
return nil, errTestFailure
|
||||
AddTool(s, &Tool{Name: "fail", InputSchema: &jsonschema.Schema{}},
|
||||
func(context.Context, *ServerRequest[*CallToolParams], map[string]any) (*CallToolResult, any, error) {
|
||||
return nil, nil, errTestFailure
|
||||
})
|
||||
s.AddPrompt(codeReviewPrompt, codReviewPromptHandler)
|
||||
s.AddPrompt(&Prompt{Name: "fail"}, func(_ context.Context, _ *ServerSession, _ *GetPromptParams) (*GetPromptResult, error) {
|
||||
|
|
@ -663,18 +663,18 @@ func TestCancellation(t *testing.T) {
|
|||
start = make(chan struct{})
|
||||
cancelled = make(chan struct{}, 1) // don't block the request
|
||||
)
|
||||
slowRequest := func(ctx context.Context, req *ServerRequest[*CallToolParams]) (*CallToolResult, error) {
|
||||
slowRequest := func(ctx context.Context, req *ServerRequest[*CallToolParams], args any) (*CallToolResult, any, error) {
|
||||
start <- struct{}{}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
cancelled <- struct{}{}
|
||||
case <-time.After(5 * time.Second):
|
||||
return nil, nil
|
||||
return nil, nil, nil
|
||||
}
|
||||
return nil, nil
|
||||
return nil, nil, nil
|
||||
}
|
||||
cs, _ := basicConnection(t, func(s *Server) {
|
||||
AddTool(s, &Tool{Name: "slow"}, slowRequest)
|
||||
AddTool(s, &Tool{Name: "slow", InputSchema: &jsonschema.Schema{}}, slowRequest)
|
||||
})
|
||||
defer cs.Close()
|
||||
|
||||
|
|
@ -852,7 +852,7 @@ func traceCalls[S Session](w io.Writer, prefix string) Middleware {
|
|||
}
|
||||
}
|
||||
|
||||
func nopHandler(context.Context, *ServerRequest[*CallToolParamsFor[map[string]any]]) (*CallToolResult, error) {
|
||||
func nopHandler(context.Context, *ServerRequest[*CallToolParams]) (*CallToolResult, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
|
|
@ -1015,11 +1015,11 @@ func TestSynchronousNotifications(t *testing.T) {
|
|||
}
|
||||
server := NewServer(testImpl, serverOpts)
|
||||
cs, ss := basicClientServerConnection(t, client, server, func(s *Server) {
|
||||
AddTool(s, &Tool{Name: "tool"}, func(ctx context.Context, req *ServerRequest[*CallToolParams]) (*CallToolResult, error) {
|
||||
AddTool(s, &Tool{Name: "tool"}, func(ctx context.Context, req *ServerRequest[*CallToolParams], args any) (*CallToolResult, any, error) {
|
||||
if !rootsChanged.Load() {
|
||||
return nil, fmt.Errorf("didn't get root change notification")
|
||||
return nil, nil, fmt.Errorf("didn't get root change notification")
|
||||
}
|
||||
return new(CallToolResult), nil
|
||||
return new(CallToolResult), nil, nil
|
||||
})
|
||||
})
|
||||
|
||||
|
|
@ -1064,13 +1064,13 @@ func TestNoDistributedDeadlock(t *testing.T) {
|
|||
}
|
||||
client := NewClient(testImpl, clientOpts)
|
||||
cs, _ := basicClientServerConnection(t, client, nil, func(s *Server) {
|
||||
AddTool(s, &Tool{Name: "tool1"}, func(ctx context.Context, req *ServerRequest[*CallToolParams]) (*CallToolResult, error) {
|
||||
AddTool(s, &Tool{Name: "tool1"}, func(ctx context.Context, req *ServerRequest[*CallToolParams], args any) (*CallToolResult, any, error) {
|
||||
req.Session.CreateMessage(ctx, new(CreateMessageParams))
|
||||
return new(CallToolResult), nil
|
||||
return new(CallToolResult), nil, nil
|
||||
})
|
||||
AddTool(s, &Tool{Name: "tool2"}, func(ctx context.Context, req *ServerRequest[*CallToolParams]) (*CallToolResult, error) {
|
||||
AddTool(s, &Tool{Name: "tool2"}, func(ctx context.Context, req *ServerRequest[*CallToolParams], args any) (*CallToolResult, any, error) {
|
||||
req.Session.Ping(ctx, nil)
|
||||
return new(CallToolResult), nil
|
||||
return new(CallToolResult), nil, nil
|
||||
})
|
||||
})
|
||||
defer cs.Close()
|
||||
|
|
|
|||
Loading…
Reference in a new issue