Skip to content

Commit e5c53ae

Browse files
committed
fix(inventory): preserve instructions in ForMCPRequest incl. server/discover
ForMCPRequest dropped the generated instructions when narrowing the per-request inventory, so HTTP server/discover (and initialize) returned empty instructions even though the full inventory had them. Preserve instructions on the copy and treat server/discover like initialize. Fixes discover<->initialize parity flagged on go-sdk#1034 (root cause was here, not the SDK). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 4b6d886 commit e5c53ae

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

pkg/inventory/registry.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func (r *Inventory) UnrecognizedToolsets() []string {
7171
// MCP method constants for use with ForMCPRequest.
7272
const (
7373
MCPMethodInitialize = "initialize"
74+
MCPMethodDiscover = "server/discover"
7475
MCPMethodToolsList = "tools/list"
7576
MCPMethodToolsCall = "tools/call"
7677
MCPMethodResourcesList = "resources/list"
@@ -89,7 +90,7 @@ const (
8990
// - itemName: Name of specific item for call/get methods (tool name, resource URI, or prompt name)
9091
//
9192
// Returns a new Registry containing only the items relevant to the request:
92-
// - MCPMethodInitialize: Empty (capabilities are set via ServerOptions, not registration)
93+
// - MCPMethodInitialize / MCPMethodDiscover: Empty items (capabilities from ServerOptions; instructions preserved)
9394
// - MCPMethodToolsList: All available tools (no resources/prompts)
9495
// - MCPMethodToolsCall: Only the named tool
9596
// - MCPMethodResourcesList, MCPMethodResourcesTemplatesList: All available resources (no tools/prompts)
@@ -114,6 +115,7 @@ func (r *Inventory) ForMCPRequest(method string, itemName string) *Inventory {
114115
featureChecker: r.featureChecker,
115116
filters: r.filters, // shared, not modified
116117
unrecognizedToolsets: r.unrecognizedToolsets,
118+
instructions: r.instructions, // server identity; preserved for all methods
117119
}
118120

119121
// Helper to clear all item types
@@ -124,7 +126,10 @@ func (r *Inventory) ForMCPRequest(method string, itemName string) *Inventory {
124126
}
125127

126128
switch method {
127-
case MCPMethodInitialize:
129+
case MCPMethodInitialize, MCPMethodDiscover:
130+
// Both handshakes register no items; capabilities come from ServerOptions
131+
// and instructions are preserved via the copy above (SEP-2575 discover
132+
// must surface the same server identity as initialize).
128133
clearAll()
129134
case MCPMethodToolsList:
130135
result.resourceTemplates, result.prompts = nil, nil

pkg/inventory/registry_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,3 +2268,16 @@ func TestShouldStripMCPAppsMetadata(t *testing.T) {
22682268
})
22692269
}
22702270
}
2271+
2272+
func TestForMCPRequest_PreservesInstructions(t *testing.T) {
2273+
reg := mustBuild(t, NewBuilder().
2274+
SetTools([]ServerTool{mockTool("tool1", "repos", true)}).
2275+
WithToolsets([]string{"all"}).
2276+
WithServerInstructions())
2277+
want := reg.Instructions()
2278+
require.NotEmpty(t, want, "expected base inventory to generate instructions")
2279+
for _, m := range []string{MCPMethodDiscover, MCPMethodInitialize, MCPMethodToolsList} {
2280+
require.Equal(t, want, reg.ForMCPRequest(m, "").Instructions(),
2281+
"instructions must be preserved for %s (server identity)", m)
2282+
}
2283+
}

0 commit comments

Comments
 (0)