Skip to content

Commit dd2e40e

Browse files
committed
fix(mcp): prevent resource key collisions
1 parent 947e001 commit dd2e40e

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

packages/opencode/src/mcp/catalog.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export function fetch<T extends { name: string }>(
102102
const sanitizedClient = sanitize(clientName)
103103
return Object.fromEntries(
104104
items.map((item) => [
105-
key ? clientName + ":" + key(item) : sanitizedClient + ":" + sanitize(item.name),
105+
key ? encodeURIComponent(clientName) + ":" + key(item) : sanitizedClient + ":" + sanitize(item.name),
106106
{ ...item, client: clientName },
107107
]),
108108
)

packages/opencode/test/mcp/lifecycle.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,28 @@ it.instance(
422422
{ config: { mcp: {} } },
423423
)
424424

425+
it.instance(
426+
"keeps resource identities distinct when server names contain separators",
427+
() =>
428+
MCP.Service.use((mcp: MCPNS.Interface) =>
429+
Effect.gen(function* () {
430+
lastCreatedClientName = "a"
431+
getOrCreateClientState("a").resources = [{ name: "first", uri: "b:c" }]
432+
getOrCreateClientState("a").resourceTemplates = [{ name: "first", uriTemplate: "b:{id}" }]
433+
yield* mcp.add("a", { type: "local", command: ["echo", "test"] })
434+
435+
lastCreatedClientName = "a:b"
436+
getOrCreateClientState("a:b").resources = [{ name: "second", uri: "c" }]
437+
getOrCreateClientState("a:b").resourceTemplates = [{ name: "second", uriTemplate: "{id}" }]
438+
yield* mcp.add("a:b", { type: "local", command: ["echo", "test"] })
439+
440+
expect(Object.keys(yield* mcp.resources()).sort()).toEqual(["a%3Ab:c", "a:b:c"])
441+
expect(Object.keys(yield* mcp.resourceTemplates()).sort()).toEqual(["a%3Ab:{id}", "a:b:{id}"])
442+
}),
443+
),
444+
{ config: { mcp: {} } },
445+
)
446+
425447
it.instance(
426448
"follows empty cursors",
427449
() =>

0 commit comments

Comments
 (0)