Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/app-bridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [] });
Expand Down
31 changes: 31 additions & 0 deletions src/app-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
type CreateMessageResult,
type CreateMessageResultWithTools,
type EmptyResult,
type GetPromptRequest,
type GetPromptResult,
type Implementation,
type JSONRPCRequest,
type ListPromptsRequest,
Expand Down Expand Up @@ -1579,6 +1581,35 @@ export class AppBridge extends Protocol<BaseContext> {
});
}

/**
* 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<GetPromptResult>;
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.
*
Expand Down