diff --git a/src/app-bridge.test.ts b/src/app-bridge.test.ts index a87b0e7db..a89177eb6 100644 --- a/src/app-bridge.test.ts +++ b/src/app-bridge.test.ts @@ -2356,6 +2356,42 @@ describe("App <-> AppBridge integration", () => { expect(result.prompts).toEqual(prompts); }); + it("routes prompts/get through the host's explicit handler", async () => { + const params = { name: "welcome", arguments: { name: "Ada" } }; + const result = { + messages: [ + { + role: "user" as const, + content: { type: "text" as const, text: "Hello, Ada" }, + }, + ], + }; + + await bridge.connect(bridgeTransport); + await app.connect(appTransport); + await expect( + app.request({ method: "prompts/get", params }), + ).rejects.toMatchObject({ + code: -32601, + }); + + bridge.ongetprompt = async (receivedParams) => { + expect(receivedParams).toEqual(params); + return result; + }; + + expect(await app.request({ method: "prompts/get", params })).toEqual( + result, + ); + + bridge.ongetprompt = undefined; + await expect( + app.request({ method: "prompts/get", params }), + ).rejects.toMatchObject({ + code: -32601, + }); + }); + it("sendToolListChanged sends notification to app", async () => { const receivedNotifications: unknown[] = []; bridge.oncalltool = async () => ({ content: [] }); diff --git a/src/app-bridge.ts b/src/app-bridge.ts index 1ac4db485..07d53513f 100644 --- a/src/app-bridge.ts +++ b/src/app-bridge.ts @@ -9,6 +9,8 @@ import { type CreateMessageResult, type CreateMessageResultWithTools, type EmptyResult, + type GetPromptRequest, + type GetPromptResult, type Implementation, type JSONRPCRequest, type ListPromptsRequest, @@ -1579,6 +1581,35 @@ export class AppBridge extends Protocol { }); } + /** + * Register a host handler that retrieves a prompt for the view. + * + * The host can authorize the request and use the prompt name and arguments + * to retrieve it from the MCP server. Hosts opt in by setting this handler. + */ + private _ongetprompt?: ( + params: GetPromptRequest["params"], + extra: RequestHandlerExtra, + ) => Promise; + get ongetprompt() { + return this._ongetprompt; + } + set ongetprompt(callback: AppBridge["_ongetprompt"]) { + this.warnIfRequestHandlerReplaced( + "ongetprompt", + this._ongetprompt, + callback, + ); + if (callback) { + this.replaceRequestHandler("prompts/get", (request, extra) => + callback(request.params, extra), + ); + } else if (this._ongetprompt) { + this.removeRequestHandler("prompts/get"); + } + this._ongetprompt = callback; + } + /** * Notify the view that the MCP server's prompt list has changed. *