on main help sign in
fix(mcp): gracefully handle Method not found for resources/list (#2239)
dff955f0xarcher
internal/agent/tools/mcp/resources.go+13
@@ -2,11 +2,13 @@ package mcp
2
2
3import (
3import (
4 "context"
4 "context"
5+ "errors"
5 "iter"
6 "iter"
6 "log/slog"
7 "log/slog"
7
8
8 "github.com/charmbracelet/crush/internal/config"
9 "github.com/charmbracelet/crush/internal/config"
9 "github.com/charmbracelet/crush/internal/csync"
10 "github.com/charmbracelet/crush/internal/csync"
11+ "github.com/modelcontextprotocol/go-sdk/jsonrpc"
10 "github.com/modelcontextprotocol/go-sdk/mcp"
12 "github.com/modelcontextprotocol/go-sdk/mcp"
11)
13)
12
14
@@ -81,11 +83,22 @@ func getResources(ctx context.Context, c *ClientSession) ([]*Resource, error) {
81 }
83 }
82 result, err := c.ListResources(ctx, &mcp.ListResourcesParams{})
84 result, err := c.ListResources(ctx, &mcp.ListResourcesParams{})
83 if err != nil {
85 if err != nil {
86+ // Handle "Method not found" errors from MCP servers that don't support resources/list
87+ if isMethodNotFoundError(err) {
88+ slog.Warn("MCP server does not support resources/list", "error", err)
89+ return nil, nil
90+ }
84 return nil, err
91 return nil, err
85 }
92 }
86 return result.Resources, nil
93 return result.Resources, nil
87}
94}
88
95
96+// isMethodNotFoundError checks if the error is a JSON-RPC "Method not found" error.
97+func isMethodNotFoundError(err error) bool {
98+ var rpcErr *jsonrpc.Error
99+ return errors.As(err, &rpcErr) && rpcErr != nil && rpcErr.Code == jsonrpc.CodeMethodNotFound
100+}
101+
89func updateResources(name string, resources []*Resource) int {
102func updateResources(name string, resources []*Resource) int {
90 if len(resources) == 0 {
103 if len(resources) == 0 {
91 allResources.Del(name)
104 allResources.Del(name)