Skip to content

Commit a8d0114

Browse files
committed
fix(blocked-tools): close startup tool-count gap, hoist auth ctx, harden retrieve_tools test
Address review feedback on PR smart-mcp-proxy#447: 1. handleListUpstreams previously fell back to live ListTools only when p.mainServer == nil (test/standalone). In production the getVisibleToolCount path uses the supervisor's StateView snapshot, which is empty during startup before the supervisor hydrates - so the UI would show "0 tools" for a freshly-connected server with healthy tools. Restored the live ListTools fallback in production too, filtered by isToolCallable so disabled tools still don't inflate the count. 2. handleRetrieveTools called auth.AuthContextFromContext(ctx) inside the per-result loop. Hoisted it (and the IsAdmin check) above the loop - it's a per-request value and the lookup is repeated work for every search result. 3. TestRetrieveTools_ExcludesDisabledToolsPreRanking previously silent-passed if the response had no "tools" field. The query ("context7 resolve library id") only matched the disabled context7 tool, so the enabled github tool wasn't a positive control - the test verified nothing once the disabled tool was filtered out. Rewrote both tools to share the same description so BM25 returns both, then asserted: - the enabled tool MUST appear (positive control catches an over- eager filter regression), - the disabled tool MUST NOT appear (negative control). Also dropped the silent early-return in favor of require.True on the tools field. Verification: - go test ./internal/server -count=1 -timeout 10m PASS (434s) - go vet ./... clean Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 1f864f6 commit a8d0114

4 files changed

Lines changed: 45 additions & 22 deletions

File tree

‎internal/server/mcp.go‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,12 @@ func (p *MCPProxyServer) handleRetrieveToolsWithMode(ctx context.Context, reques
10381038

10391039
// Spec 028 + blocked tool semantics: filter results to only include callable tools
10401040
// from servers the agent can access. Disabled/blocked tools are treated as non-existent
1041-
// for runtime discovery.
1041+
// for runtime discovery. The auth context is hoisted out of the loop because it's a
1042+
// per-request value and AuthContextFromContext does a context.Value lookup we don't
1043+
// want to repeat per result.
1044+
authCtx := auth.AuthContextFromContext(ctx)
1045+
enforceAgentScope := authCtx != nil && !authCtx.IsAdmin()
1046+
10421047
var callableResults []*config.SearchResult
10431048
for _, result := range results {
10441049
serverName := result.Tool.ServerName
@@ -1051,10 +1056,8 @@ func (p *MCPProxyServer) handleRetrieveToolsWithMode(ctx context.Context, reques
10511056
}
10521057
}
10531058

1054-
if authCtx := auth.AuthContextFromContext(ctx); authCtx != nil && !authCtx.IsAdmin() {
1055-
if !authCtx.CanAccessServer(serverName) {
1056-
continue
1057-
}
1059+
if enforceAgentScope && !authCtx.CanAccessServer(serverName) {
1060+
continue
10581061
}
10591062

10601063
if !p.isToolCallable(serverName, toolName) {
@@ -2581,12 +2584,18 @@ func (p *MCPProxyServer) handleListUpstreams(ctx context.Context) (*mcp.CallTool
25812584
}
25822585
isConnected = connInfo.State.String() == "connected"
25832586
userLoggedOut = client.IsUserLoggedOut()
2584-
// Get visible/callable tool count (disabled tools are hidden)
2587+
// Get visible/callable tool count (disabled tools are hidden). The stateview
2588+
// snapshot may be empty during startup before the supervisor has hydrated it,
2589+
// so fall back to a live ListTools call (filtered by isToolCallable) so the UI
2590+
// doesn't show "0 tools" for a connected server with healthy tools.
25852591
toolCount = p.getVisibleToolCount(server.Name)
2586-
if toolCount == 0 && p.mainServer == nil {
2587-
// Test/standalone fallback when runtime/supervisor is unavailable
2592+
if toolCount == 0 {
25882593
if tools, err := client.ListTools(context.Background()); err == nil {
2589-
toolCount = len(tools)
2594+
for _, tool := range tools {
2595+
if p.isToolCallable(server.Name, tool.Name) {
2596+
toolCount++
2597+
}
2598+
}
25902599
}
25912600
}
25922601

‎internal/server/mcp_blocked_tools_test.go‎

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,33 @@ func TestRetrieveTools_ExcludesDisabledToolsPreRanking(t *testing.T) {
4040
require.NoError(t, proxy.storage.SaveUpstreamServer(&config.ServerConfig{Name: "context7", Enabled: true}))
4141
require.NoError(t, proxy.storage.SaveUpstreamServer(&config.ServerConfig{Name: "github", Enabled: true}))
4242

43+
// Mark the context7 tool as approved-but-disabled. The github tool stays
44+
// enabled (no approval row needed - default callable for an enabled server).
4345
require.NoError(t, proxy.storage.SaveToolApproval(&storage.ToolApprovalRecord{
4446
ServerName: "context7",
45-
ToolName: "resolve-library-id",
47+
ToolName: "library-lookup",
4648
Status: storage.ToolApprovalStatusApproved,
4749
Disabled: true,
4850
}))
4951

52+
// Both tools share keywords from the query so BM25 returns both - that way
53+
// we get a real positive control: the github tool MUST survive the filter.
5054
require.NoError(t, proxy.index.IndexTool(&config.ToolMetadata{
51-
Name: "context7:resolve-library-id",
55+
Name: "context7:library-lookup",
5256
ServerName: "context7",
53-
Description: "Resolve library IDs",
57+
Description: "library documentation lookup helper",
5458
ParamsJSON: "{\"type\":\"object\"}",
5559
}))
5660
require.NoError(t, proxy.index.IndexTool(&config.ToolMetadata{
57-
Name: "github:get_file_contents",
61+
Name: "github:library-lookup",
5862
ServerName: "github",
59-
Description: "Get file contents",
63+
Description: "library documentation lookup helper",
6064
ParamsJSON: "{\"type\":\"object\"}",
6165
}))
6266

6367
req := mcp.CallToolRequest{}
6468
req.Params.Arguments = map[string]interface{}{
65-
"query": "context7 resolve library id",
69+
"query": "library lookup",
6670
"limit": float64(10),
6771
}
6872

@@ -76,18 +80,25 @@ func TestRetrieveTools_ExcludesDisabledToolsPreRanking(t *testing.T) {
7680
require.NoError(t, json.Unmarshal([]byte(text), &payload))
7781

7882
toolsValue, exists := payload["tools"]
79-
if !exists || toolsValue == nil {
80-
return
81-
}
83+
require.True(t, exists, "retrieve_tools response must contain a tools array")
84+
require.NotNil(t, toolsValue, "tools array must not be nil")
8285

8386
toolsRaw, ok := toolsValue.([]interface{})
84-
require.True(t, ok)
87+
require.True(t, ok, "tools must be a JSON array")
8588

89+
names := make([]string, 0, len(toolsRaw))
8690
for _, item := range toolsRaw {
8791
tool := item.(map[string]interface{})
88-
name := tool["name"].(string)
89-
assert.NotEqual(t, "context7:resolve-library-id", name)
92+
names = append(names, tool["name"].(string))
9093
}
94+
95+
// Positive control: the enabled tool must survive ranking + filtering. If the
96+
// disabled tool's filtering also clobbered the enabled one (regression), this
97+
// catches it. Negative control: the disabled tool must be gone.
98+
assert.Contains(t, names, "github:library-lookup",
99+
"enabled tool must survive ranking and filtering")
100+
assert.NotContains(t, names, "context7:library-lookup",
101+
"disabled tool must be filtered out before ranking")
91102
}
92103

93104
func TestCallBlockedTool_ReturnsToolBlocked(t *testing.T) {

‎oas/docs.go‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎oas/swagger.yaml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,9 @@ components:
13241324
contracts.QuarantineStats:
13251325
description: Tool quarantine metrics for this server
13261326
properties:
1327+
blocked_count:
1328+
description: Number of disabled (blocked) tools
1329+
type: integer
13271330
changed_count:
13281331
description: Number of tools whose description/schema changed since approval
13291332
type: integer

0 commit comments

Comments
 (0)