Skip to content

Commit 1d8e2d9

Browse files
committed
fix(core): keep cached MCP prompts when a refresh fails
1 parent 808588e commit 1d8e2d9

2 files changed

Lines changed: 73 additions & 2 deletions

File tree

‎packages/core/src/mcp/index.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,10 @@ export const layer = (options?: Options) =>
303303
}),
304304
)
305305

306+
// A failed listing is not an empty one: like refreshTools, a refresh that errors keeps the prompts it
307+
// already had, so a transient server error cannot publish an empty command list.
306308
const refreshPrompts = (name: ServerName, entry: ServerEntry, connection: McpClient.Connection) =>
307309
connection.prompts().pipe(
308-
Effect.orElseSucceed(() => []),
309310
Effect.map((prompts) => {
310311
entry.prompts = prompts.map((prompt): Prompt => ({ ...prompt, server: name }))
311312
}),

‎packages/core/test/mcp.test.ts‎

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ function resourceServer(
9090
modern?: boolean
9191
resources?: boolean
9292
listChanged?: boolean
93+
/** Advertise prompts listChanged, which is what makes the client listen for a prompts refresh. */
94+
promptListChanged?: boolean
9395
emptyElicitation?: boolean
9496
urlElicitation?: boolean
9597
respond?: (request: Request) => Response | undefined | Promise<Response | undefined>
@@ -130,7 +132,7 @@ function resourceServer(
130132
{
131133
capabilities: {
132134
tools: {},
133-
prompts: {},
135+
prompts: input.promptListChanged ? { listChanged: true } : {},
134136
...(input.resources === false ? {} : { resources: { listChanged: input.listChanged } }),
135137
},
136138
instructions: "Use the resources tools.",
@@ -248,6 +250,7 @@ function resourceServer(
248250
clientVersion: () => current.protocol.getClientVersion(),
249251
sendResourceListChanged: () =>
250252
modern ? Promise.resolve(modern.notify.resourcesChanged()) : current.protocol.sendResourceListChanged(),
253+
sendPromptListChanged: () => current.protocol.sendPromptListChanged(),
251254
completeElicitation: () => current.protocol.createElicitationCompletionNotifier("elicitation-test")(),
252255
restart: async () => {
253256
await current.protocol.close().catch(() => {})
@@ -1555,6 +1558,73 @@ it.live("discovers and reads MCP resources through Code Mode", () =>
15551558
}),
15561559
)
15571560

1561+
/**
1562+
* Answers one server method with a JSON-RPC error once armed, and counts the requests it saw. Everything
1563+
* else still reaches the server, so only the listing under test fails.
1564+
*/
1565+
function failingMethod(method: string, message: string) {
1566+
const state = { armed: false, requests: 0 }
1567+
return {
1568+
arm: () => {
1569+
state.armed = true
1570+
},
1571+
requests: () => state.requests,
1572+
respond: async (request: Request) => {
1573+
if (request.method !== "POST") return undefined
1574+
const body = (await request.clone().json()) as { method?: string; id?: number }
1575+
if (body.method !== method) return undefined
1576+
state.requests += 1
1577+
if (!state.armed) return undefined
1578+
return new Response(JSON.stringify({ jsonrpc: "2.0", id: body.id ?? 0, error: { code: -32603, message } }), {
1579+
status: 200,
1580+
headers: { "content-type": "application/json" },
1581+
})
1582+
},
1583+
}
1584+
}
1585+
1586+
test("keeps cached MCP prompts when a prompts refresh fails", async () => {
1587+
await Effect.runPromise(
1588+
Effect.scoped(
1589+
Effect.gen(function* () {
1590+
const broken = failingMethod("prompts/list", "prompt registry unavailable")
1591+
const server = yield* resourceServer({ promptListChanged: true, respond: broken.respond })
1592+
yield* Effect.gen(function* () {
1593+
const service = yield* Mcp.Service
1594+
yield* settled(service)
1595+
// Prompts load in the background once the connection settles, so wait for the first list.
1596+
const greeted = service.prompts().pipe(
1597+
Effect.map((prompts) => prompts.map((prompt) => prompt.name)),
1598+
Effect.filterOrFail(
1599+
(names) => names.join() === "greet",
1600+
(names) => new Error(`prompts were ${JSON.stringify(names)}`),
1601+
),
1602+
Effect.retry({ times: 100, schedule: Schedule.spaced("10 millis") }),
1603+
)
1604+
yield* greeted
1605+
1606+
broken.arm()
1607+
yield* Effect.promise(server.sendPromptListChanged)
1608+
yield* Effect.sync(() => broken.requests()).pipe(
1609+
Effect.filterOrFail(
1610+
(count) => count >= 2,
1611+
() => new Error("the announced prompts refresh never reached the server"),
1612+
),
1613+
Effect.retry({ times: 100, schedule: Schedule.spaced("10 millis") }),
1614+
)
1615+
// Let the failed response settle: the refresh runs in the background either way.
1616+
yield* Effect.promise(() => Bun.sleep(50))
1617+
1618+
yield* greeted
1619+
expect((yield* service.servers()).find((server) => server.name === "resources")?.status).toEqual({
1620+
status: "connected",
1621+
})
1622+
}).pipe(Effect.provide(resourceMcpLayer(server.url)))
1623+
}),
1624+
),
1625+
)
1626+
})
1627+
15581628
test("adds, disconnects, and reconnects MCP servers at runtime", async () => {
15591629
const published: string[] = []
15601630
await Effect.runPromise(

0 commit comments

Comments
 (0)