@@ -90,6 +90,8 @@ function resourceServer( |
90 | 90 | modern?: boolean |
91 | 91 | resources?: boolean |
92 | 92 | listChanged?: boolean |
| 93 | + /** Advertise prompts listChanged, which is what makes the client listen for a prompts refresh. */ |
| 94 | + promptListChanged?: boolean |
93 | 95 | emptyElicitation?: boolean |
94 | 96 | urlElicitation?: boolean |
95 | 97 | respond?: (request: Request) => Response | undefined | Promise<Response | undefined> |
@@ -130,7 +132,7 @@ function resourceServer( |
130 | 132 | { |
131 | 133 | capabilities: { |
132 | 134 | tools: {}, |
133 | | - prompts: {}, |
| 135 | + prompts: input.promptListChanged ? { listChanged: true } : {}, |
134 | 136 | ...(input.resources === false ? {} : { resources: { listChanged: input.listChanged } }), |
135 | 137 | }, |
136 | 138 | instructions: "Use the resources tools.", |
@@ -248,6 +250,7 @@ function resourceServer( |
248 | 250 | clientVersion: () => current.protocol.getClientVersion(), |
249 | 251 | sendResourceListChanged: () => |
250 | 252 | modern ? Promise.resolve(modern.notify.resourcesChanged()) : current.protocol.sendResourceListChanged(), |
| 253 | + sendPromptListChanged: () => current.protocol.sendPromptListChanged(), |
251 | 254 | completeElicitation: () => current.protocol.createElicitationCompletionNotifier("elicitation-test")(), |
252 | 255 | restart: async () => { |
253 | 256 | await current.protocol.close().catch(() => {}) |
@@ -1555,6 +1558,73 @@ it.live("discovers and reads MCP resources through Code Mode", () => |
1555 | 1558 | }), |
1556 | 1559 | ) |
1557 | 1560 |
|
| 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 | + |
1558 | 1628 | test("adds, disconnects, and reconnects MCP servers at runtime", async () => { |
1559 | 1629 | const published: string[] = [] |
1560 | 1630 | await Effect.runPromise( |
|