Skip to content

chore: separate the LoadSession function in the MCP loader #966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions pkg/mcp/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type ServerConfig struct {
BaseURL string `json:"baseURL,omitempty"`
Headers []string `json:"headers"`
Scope string `json:"scope"`
AllowedTools []string `json:"allowedTools"`
}

func (s *ServerConfig) GetBaseURL() string {
Expand Down Expand Up @@ -99,18 +100,31 @@ func (l *Local) Load(ctx context.Context, tool types.Tool) (result []types.Tool,
}

for server := range maps.Keys(servers.MCPServers) {
session, err := l.loadSession(servers.MCPServers[server])
tools, err := l.LoadTools(ctx, servers.MCPServers[server], tool.Name)
if err != nil {
return nil, fmt.Errorf("failed to load MCP session for server %s: %w", server, err)
}

return l.sessionToTools(ctx, session, tool.Name)
return tools, nil
}

// This should never happen, but just in case
return nil, fmt.Errorf("no MCP server configuration found in tool instructions: %s", configData)
}

func (l *Local) LoadTools(ctx context.Context, server ServerConfig, toolName string) ([]types.Tool, error) {
allowedTools := server.AllowedTools
// Reset so we don't start a new MCP server, no reason to if one is already running and the allowed tools change.
server.AllowedTools = nil

session, err := l.loadSession(server)
if err != nil {
return nil, err
}

return l.sessionToTools(ctx, session, toolName, allowedTools)
}

func (l *Local) Close() error {
if l == nil {
return nil
Expand Down Expand Up @@ -139,7 +153,9 @@ func (l *Local) Close() error {
return errors.Join(errs...)
}

func (l *Local) sessionToTools(ctx context.Context, session *Session, toolName string) ([]types.Tool, error) {
func (l *Local) sessionToTools(ctx context.Context, session *Session, toolName string, allowedTools []string) ([]types.Tool, error) {
allToolsAllowed := len(allowedTools) == 0 || slices.Contains(allowedTools, "*")

tools, err := session.Client.ListTools(ctx, mcp.ListToolsRequest{})
if err != nil {
return nil, fmt.Errorf("failed to list tools: %w", err)
Expand All @@ -149,6 +165,10 @@ func (l *Local) sessionToTools(ctx context.Context, session *Session, toolName s
var toolNames []string

for _, tool := range tools.Tools {
if !allToolsAllowed && !slices.Contains(allowedTools, tool.Name) {
continue
}

var schema openapi3.Schema

schemaData, err := json.Marshal(tool.InputSchema)
Expand Down
12 changes: 6 additions & 6 deletions pkg/tests/runner2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ name: mcp
],
"type": "object"
},
"instructions": "#!sys.mcp.invoke.append_insight 607ca64476abf0288ef49061557243e43735fd4de4bc5fdcd51d93049ffa023e",
"instructions": "#!sys.mcp.invoke.append_insight c358c2eb93fa9a98631cd9e4f324d7b59f56aee11c7ae32a00984ad5844dc32c",
"id": "inline:append_insight",
"localTools": {
"append_insight": "inline:append_insight",
Expand Down Expand Up @@ -346,7 +346,7 @@ name: mcp
],
"type": "object"
},
"instructions": "#!sys.mcp.invoke.create_table 607ca64476abf0288ef49061557243e43735fd4de4bc5fdcd51d93049ffa023e",
"instructions": "#!sys.mcp.invoke.create_table c358c2eb93fa9a98631cd9e4f324d7b59f56aee11c7ae32a00984ad5844dc32c",
"id": "inline:create_table",
"localTools": {
"append_insight": "inline:append_insight",
Expand Down Expand Up @@ -379,7 +379,7 @@ name: mcp
],
"type": "object"
},
"instructions": "#!sys.mcp.invoke.describe_table 607ca64476abf0288ef49061557243e43735fd4de4bc5fdcd51d93049ffa023e",
"instructions": "#!sys.mcp.invoke.describe_table c358c2eb93fa9a98631cd9e4f324d7b59f56aee11c7ae32a00984ad5844dc32c",
"id": "inline:describe_table",
"localTools": {
"append_insight": "inline:append_insight",
Expand All @@ -403,7 +403,7 @@ name: mcp
"arguments": {
"type": "object"
},
"instructions": "#!sys.mcp.invoke.list_tables 607ca64476abf0288ef49061557243e43735fd4de4bc5fdcd51d93049ffa023e",
"instructions": "#!sys.mcp.invoke.list_tables c358c2eb93fa9a98631cd9e4f324d7b59f56aee11c7ae32a00984ad5844dc32c",
"id": "inline:list_tables",
"localTools": {
"append_insight": "inline:append_insight",
Expand Down Expand Up @@ -505,7 +505,7 @@ name: mcp
],
"type": "object"
},
"instructions": "#!sys.mcp.invoke.read_query 607ca64476abf0288ef49061557243e43735fd4de4bc5fdcd51d93049ffa023e",
"instructions": "#!sys.mcp.invoke.read_query c358c2eb93fa9a98631cd9e4f324d7b59f56aee11c7ae32a00984ad5844dc32c",
"id": "inline:read_query",
"localTools": {
"append_insight": "inline:append_insight",
Expand Down Expand Up @@ -538,7 +538,7 @@ name: mcp
],
"type": "object"
},
"instructions": "#!sys.mcp.invoke.write_query 607ca64476abf0288ef49061557243e43735fd4de4bc5fdcd51d93049ffa023e",
"instructions": "#!sys.mcp.invoke.write_query c358c2eb93fa9a98631cd9e4f324d7b59f56aee11c7ae32a00984ad5844dc32c",
"id": "inline:write_query",
"localTools": {
"append_insight": "inline:append_insight",
Expand Down