Skip to content

fix(MCPServer): Session tool handler not used due to variable shadowing #242

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 1 commit into from
May 4, 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
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ func (s *MCPServer) handleToolCall(

session := ClientSessionFromContext(ctx)
if session != nil {
if sessionWithTools, ok := session.(SessionWithTools); ok {
if sessionWithTools, typeAssertOk := session.(SessionWithTools); typeAssertOk {
if sessionTools := sessionWithTools.GetSessionTools(); sessionTools != nil {
var sessionOk bool
tool, sessionOk = sessionTools[request.Params.Name]
Expand Down
59 changes: 59 additions & 0 deletions server/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"context"
"encoding/json"
"errors"
"sync"
"testing"
Expand Down Expand Up @@ -295,6 +296,64 @@ func TestMCPServer_AddSessionTool(t *testing.T) {
assert.Contains(t, session.GetSessionTools(), "session-tool-helper")
}

func TestMCPServer_CallSessionTool(t *testing.T) {
server := NewMCPServer("test-server", "1.0.0", WithToolCapabilities(true))

// Add global tool
server.AddTool(mcp.NewTool("test_tool"), func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
return mcp.NewToolResultText("global result"), nil
})

// Create a session
sessionChan := make(chan mcp.JSONRPCNotification, 10)
session := &sessionTestClientWithTools{
sessionID: "session-1",
notificationChannel: sessionChan,
initialized: true,
}

// Register the session
err := server.RegisterSession(context.Background(), session)
require.NoError(t, err)

// Add session-specific tool with the same name to override the global tool
err = server.AddSessionTool(
session.SessionID(),
mcp.NewTool("test_tool"),
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
return mcp.NewToolResultText("session result"), nil
},
)
require.NoError(t, err)

// Call the tool using session context
sessionCtx := server.WithContext(context.Background(), session)
toolRequest := map[string]interface{}{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": map[string]interface{}{
"name": "test_tool",
},
}
requestBytes, err := json.Marshal(toolRequest)
if err != nil {
t.Fatalf("Failed to marshal tool request: %v", err)
}

response := server.HandleMessage(sessionCtx, requestBytes)
resp, ok := response.(mcp.JSONRPCResponse)
assert.True(t, ok)

callToolResult, ok := resp.Result.(mcp.CallToolResult)
assert.True(t, ok)

// Since we specify a tool with the same name for current session, the expected text should be "session result"
if text := callToolResult.Content[0].(mcp.TextContent).Text; text != "session result" {
t.Errorf("Expected result 'session result', got %q", text)
}
}

func TestMCPServer_DeleteSessionTools(t *testing.T) {
server := NewMCPServer("test-server", "1.0.0", WithToolCapabilities(true))
ctx := context.Background()
Expand Down