-
Notifications
You must be signed in to change notification settings - Fork 233
fix(mcp): reset registeredTools between reloads to prevent false tool name collisions #2625
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
asoorm
merged 2 commits into
main
from
ahmet/eng-9158-mcp-registeredtools-grows-unbounded-across-config-reloads
Mar 12, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| package mcpserver | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/astparser" | ||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/asttransform" | ||
| "go.uber.org/zap" | ||
| "go.uber.org/zap/zapcore" | ||
| "go.uber.org/zap/zaptest/observer" | ||
| ) | ||
|
|
||
| const testSchema = ` | ||
| schema { | ||
| query: Query | ||
| } | ||
|
|
||
| type Query { | ||
| employee(id: ID!): Employee | ||
| employees: [Employee!]! | ||
| } | ||
|
|
||
| type Employee { | ||
| id: ID! | ||
| name: String! | ||
| } | ||
| ` | ||
|
|
||
| const findEmployeeOp = ` | ||
| query FindEmployee($id: ID!) { | ||
| employee(id: $id) { | ||
| id | ||
| name | ||
| } | ||
| } | ||
| ` | ||
|
|
||
| const listEmployeesOp = ` | ||
| query ListEmployees { | ||
| employees { | ||
| id | ||
| name | ||
| } | ||
| } | ||
| ` | ||
|
|
||
| const getOperationInfoOp = ` | ||
| query GetOperationInfo { | ||
| employees { | ||
| id | ||
| name | ||
| } | ||
| } | ||
| ` | ||
|
|
||
| func writeOperationFiles(t *testing.T, dir string, files map[string]string) { | ||
| t.Helper() | ||
| for filename, content := range files { | ||
| err := os.WriteFile(filepath.Join(dir, filename), []byte(content), 0644) | ||
| require.NoError(t, err) | ||
| } | ||
| } | ||
|
|
||
| func TestReload_NoToolDuplication(t *testing.T) { | ||
| core, logs := observer.New(zapcore.DebugLevel) | ||
| logger := zap.New(core) | ||
|
|
||
| tempDir := t.TempDir() | ||
| writeOperationFiles(t, tempDir, map[string]string{ | ||
| "FindEmployee.graphql": findEmployeeOp, | ||
| "ListEmployees.graphql": listEmployeesOp, | ||
| }) | ||
|
|
||
| schemaDoc, report := astparser.ParseGraphqlDocumentString(testSchema) | ||
| require.False(t, report.HasErrors()) | ||
| err := asttransform.MergeDefinitionWithBaseSchema(&schemaDoc) | ||
| require.NoError(t, err) | ||
|
|
||
| srv, err := NewGraphQLSchemaServer( | ||
| "http://localhost:4000/graphql", | ||
| WithLogger(logger), | ||
| WithOperationsDir(tempDir), | ||
| WithOmitToolNamePrefix(true), | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| // First load | ||
| err = srv.Reload(&schemaDoc) | ||
| require.NoError(t, err) | ||
|
|
||
| firstLoadTools := make([]string, len(srv.registeredTools)) | ||
| copy(firstLoadTools, srv.registeredTools) | ||
|
|
||
| // Second load (simulates config reload) | ||
| err = srv.Reload(&schemaDoc) | ||
| require.NoError(t, err) | ||
|
|
||
| // registeredTools should be identical after reload — no duplicates | ||
| assert.Equal(t, firstLoadTools, srv.registeredTools, | ||
| "registered tools should be identical after reload, no duplicates") | ||
|
|
||
| // Verify no collision errors were logged | ||
| collisionLogs := logs.FilterMessage("Skipping operation due to tool name collision") | ||
| assert.Equal(t, 0, collisionLogs.Len(), | ||
| "no tool name collision errors should be logged on reload") | ||
| } | ||
|
|
||
| func TestReload_ReservedToolNameCollision(t *testing.T) { | ||
| core, logs := observer.New(zapcore.DebugLevel) | ||
| logger := zap.New(core) | ||
|
|
||
| // Create an operation whose snake_case name will be "get_operation_info", | ||
| // which collides with the reserved tool name. | ||
| tempDir := t.TempDir() | ||
| writeOperationFiles(t, tempDir, map[string]string{ | ||
| "GetOperationInfo.graphql": getOperationInfoOp, | ||
| "ListEmployees.graphql": listEmployeesOp, | ||
| }) | ||
|
|
||
| schemaDoc, report := astparser.ParseGraphqlDocumentString(testSchema) | ||
| require.False(t, report.HasErrors()) | ||
| err := asttransform.MergeDefinitionWithBaseSchema(&schemaDoc) | ||
| require.NoError(t, err) | ||
|
|
||
| srv, err := NewGraphQLSchemaServer( | ||
| "http://localhost:4000/graphql", | ||
| WithLogger(logger), | ||
| WithOperationsDir(tempDir), | ||
| WithOmitToolNamePrefix(true), | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| err = srv.Reload(&schemaDoc) | ||
| require.NoError(t, err) | ||
|
|
||
| // The operation "GetOperationInfo" (snake: "get_operation_info") should be skipped | ||
| // because it collides with the reserved tool name. | ||
| collisionLogs := logs.FilterMessage("Skipping operation due to tool name collision") | ||
| assert.Equal(t, 1, collisionLogs.Len(), | ||
| "expected exactly one collision error for reserved tool name") | ||
|
|
||
| if collisionLogs.Len() > 0 { | ||
| entry := collisionLogs.All()[0] | ||
| assert.Equal(t, zapcore.ErrorLevel, entry.Level) | ||
| assert.Equal(t, "get_operation_info", entry.ContextMap()["conflicting_tool"]) | ||
| } | ||
|
|
||
| assert.ElementsMatch(t, []string{"get_schema", "list_employees", "get_operation_info"}, srv.registeredTools) | ||
| } | ||
|
|
||
| func TestReload_PrefixModeAvoidsReservedNameCollision(t *testing.T) { | ||
| core, logs := observer.New(zapcore.DebugLevel) | ||
| logger := zap.New(core) | ||
|
|
||
| // "GetOperationInfo" snake_cases to "get_operation_info" which is a reserved name. | ||
| // With the prefix enabled, it becomes "execute_operation_get_operation_info" and no collision occurs. | ||
| tempDir := t.TempDir() | ||
| writeOperationFiles(t, tempDir, map[string]string{ | ||
| "GetOperationInfo.graphql": getOperationInfoOp, | ||
| "ListEmployees.graphql": listEmployeesOp, | ||
| }) | ||
|
|
||
| schemaDoc, report := astparser.ParseGraphqlDocumentString(testSchema) | ||
| require.False(t, report.HasErrors()) | ||
| err := asttransform.MergeDefinitionWithBaseSchema(&schemaDoc) | ||
| require.NoError(t, err) | ||
|
|
||
| srv, err := NewGraphQLSchemaServer( | ||
| "http://localhost:4000/graphql", | ||
| WithLogger(logger), | ||
| WithOperationsDir(tempDir), | ||
| WithOmitToolNamePrefix(false), | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| err = srv.Reload(&schemaDoc) | ||
| require.NoError(t, err) | ||
|
|
||
| // No collisions because the prefix disambiguates from the reserved name | ||
| collisionLogs := logs.FilterMessage("Skipping operation due to tool name collision") | ||
| assert.Equal(t, 0, collisionLogs.Len(), | ||
| "no collisions expected with tool name prefix enabled") | ||
|
|
||
| assert.ElementsMatch(t, []string{ | ||
| "get_schema", | ||
| "execute_operation_get_operation_info", | ||
| "execute_operation_list_employees", | ||
| "get_operation_info", | ||
| }, srv.registeredTools) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.