Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(mcp-apps): block widget tool calls that were never approved (#97507)

Jonathan Mielooae62d899624b
diff --git a/products/desktop/packages/core/src/mcp-apps/mcp-apps.test.ts b/products/desktop/packages/core/src/mcp-apps/mcp-apps.test.ts
index 0933d7402b6b..32eca7cf6ab7 100644
--- a/products/desktop/packages/core/src/mcp-apps/mcp-apps.test.ts
+++ b/products/desktop/packages/core/src/mcp-apps/mcp-apps.test.ts
@@ -496,3 +496,113 @@ describe("McpAppsService lazy discovery", () => {
expect(posthogClient.readResource).toHaveBeenCalledTimes(2);
});
});
+
+function makeProxyClient(
+ tools: Array<{ name: string; _meta?: { ui: Record<string, unknown> } }>,
+) {
+ return {
+ ...makeClient(),
+ listTools: vi.fn(async () => ({ tools })),
+ callTool: vi.fn(async ({ name }: { name: string }) => ({
+ content: [{ type: "text", text: `ran ${name}` }],
+ })),
+ };
+}
+
+function connectProxyClient(
+ service: McpAppsService,
+ client: ReturnType<typeof makeProxyClient>,
+) {
+ vi.spyOn(internals(service), "createConnection").mockImplementation(
+ async (c) => ({ name: c.name, client, transport: {} }),
+ );
+ return client;
+}
+
+describe("McpAppsService.proxyToolCall", () => {
+ let service: McpAppsService;
+
+ beforeEach(() => {
+ service = makeService();
+ });
+
+ it("calls a tool whose visibility includes app", async () => {
+ service.setServerConfigs([config("posthog")]);
+ const client = makeProxyClient([
+ {
+ name: "loops-review",
+ _meta: {
+ ui: { resourceUri: REVIEW_URI, visibility: ["model", "app"] },
+ },
+ },
+ ]);
+ connectProxyClient(service, client);
+
+ await expect(
+ service.proxyToolCall("posthog", "loops-review", { id: "123" }),
+ ).resolves.toEqual({
+ content: [{ type: "text", text: "ran loops-review" }],
+ });
+ expect(client.callTool).toHaveBeenCalledWith({
+ name: "loops-review",
+ arguments: { id: "123" },
+ });
+ });
+
+ it("calls a tool with a UI association and no explicit visibility", async () => {
+ service.setServerConfigs([config("posthog")]);
+ const client = makeProxyClient([
+ { name: "loops-review", _meta: { ui: { resourceUri: REVIEW_URI } } },
+ ]);
+ connectProxyClient(service, client);
+
+ await expect(
+ service.proxyToolCall("posthog", "loops-review"),
+ ).resolves.toEqual({
+ content: [{ type: "text", text: "ran loops-review" }],
+ });
+ });
+
+ it.each([
+ ["a model-only tool", "posthog", "secret-tool", "(model)"],
+ [
+ "a tool with no UI association",
+ "posthog",
+ "exec-helper",
+ "(no UI association)",
+ ],
+ [
+ "the exec tool without a UI association",
+ "posthog",
+ "exec",
+ "(no UI association)",
+ ],
+ [
+ "the exec tool name on a non-built-in server",
+ "acme",
+ "exec",
+ "(no UI association)",
+ ],
+ ])("denies %s", async (_label, serverName, toolName, reason) => {
+ service.setServerConfigs([config("posthog"), config("acme")]);
+ const client = makeProxyClient([
+ {
+ name: "secret-tool",
+ _meta: {
+ ui: {
+ resourceUri: "ui://posthog/secret.html",
+ visibility: ["model"],
+ },
+ },
+ },
+ { name: "exec-helper" },
+ { name: "exec" },
+ ]);
+ connectProxyClient(service, client);
+
+ await expect(service.proxyToolCall(serverName, toolName)).rejects.toThrow(
+ `is not accessible to apps ${reason}`,
+ );
+ expect(client.callTool).not.toHaveBeenCalled();
+ });
+});
diff --git a/products/desktop/packages/core/src/mcp-apps/mcp-apps.ts b/products/desktop/packages/core/src/mcp-apps/mcp-apps.ts
index 913ba36c7b90..33bf4e4738c8 100644
--- a/products/desktop/packages/core/src/mcp-apps/mcp-apps.ts
+++ b/products/desktop/packages/core/src/mcp-apps/mcp-apps.ts
@@ -602,12 +602,16 @@ export class McpAppsService extends TypedEventEmitter<McpAppsServiceEvents> {
toolName: string,
args?: Record<string, unknown>,
): Promise<unknown> {
- // Validate visibility: reject if tool is model-only
const toolKey = `mcp__${serverName}__${toolName}`;
- const association = this.toolAssociations.get(toolKey);
- if (association?.visibility && !association.visibility.includes("app")) {
+ const association = await this.resolveAssociation(toolKey);
+ const isAppCallable =
+ association !== undefined &&
+ (!association.visibility || association.visibility.includes("app"));
+ if (!isAppCallable) {
+ const visibility =
+ association?.visibility?.join(", ") ?? "no UI association";
throw new Error(
- `Tool "${toolName}" is not accessible to apps (visibility: ${association.visibility.join(", ")})`,
+ `Tool "${toolName}" is not accessible to apps (${visibility})`,
);
}