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
3 changes: 2 additions & 1 deletion apps/web/__tests__/unit/mcp-auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ describe("MCP OAuth authorization", () => {
true,
);
expect(isMcpRedirectUri("http://127.0.0.1:49152/callback")).toBe(true);
expect(isMcpRedirectUri("http://localhost:49152/callback")).toBe(true);
for (const uri of [
"http://chatgpt.com/connector/callback",
"http://localhost:49152/callback",
"http://localhost.evil.example:49152/callback",
"http://127.0.0.1/callback",
"https://chatgpt.com/connector/callback#fragment",
"https://user:password@chatgpt.com/callback",
Expand Down
75 changes: 51 additions & 24 deletions apps/web/__tests__/unit/mcp-server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,30 +83,31 @@ describe("hosted MCP transport", () => {
});

it("preserves browser preflight and OAuth challenge headers", async () => {
const origin = "https://chatgpt.com";
const preflight = await OPTIONS(
new Request("https://cap.so/api/mcp", {
method: "OPTIONS",
headers: { Origin: origin },
}),
);
expect(preflight.status).toBe(204);
expect(preflight.headers.get("access-control-allow-origin")).toBe(origin);
expect(preflight.headers.get("cache-control")).toBe("no-store");
const unauthorized = await POST(
new Request("https://cap.so/api/mcp", {
method: "POST",
headers: { Origin: origin, "Content-Type": "application/json" },
body: "{}",
}),
);
expect(unauthorized.status).toBe(401);
expect(unauthorized.headers.get("access-control-allow-origin")).toBe(
origin,
);
expect(unauthorized.headers.get("www-authenticate")).toContain(
"resource_metadata",
);
for (const origin of ["https://chatgpt.com", "https://grok.com"]) {
const preflight = await OPTIONS(
new Request("https://cap.so/api/mcp", {
method: "OPTIONS",
headers: { Origin: origin },
}),
);
expect(preflight.status).toBe(204);
expect(preflight.headers.get("access-control-allow-origin")).toBe(origin);
expect(preflight.headers.get("cache-control")).toBe("no-store");
const unauthorized = await POST(
new Request("https://cap.so/api/mcp", {
method: "POST",
headers: { Origin: origin, "Content-Type": "application/json" },
body: "{}",
}),
);
expect(unauthorized.status).toBe(401);
expect(unauthorized.headers.get("access-control-allow-origin")).toBe(
origin,
);
expect(unauthorized.headers.get("www-authenticate")).toContain(
"resource_metadata",
);
}
});

it("keeps host, origin, media type, and body limits ahead of MCP dispatch", async () => {
Expand Down Expand Up @@ -150,6 +151,32 @@ describe("hosted MCP transport", () => {
expect(listBody).toContain("caps_list");
expect(listBody).toContain("caps_get");
expect(listBody).toContain("caps_context");
expect(listBody).toContain('"openWorldHint":false');
const data = listBody.split("\n").find((line) => line.startsWith("data: "));
expect(data).toBeDefined();
const tools = JSON.parse(data?.slice(6) ?? "").result.tools;
expect(tools).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: "caps_list",
annotations: expect.objectContaining({
title: "List Cap recordings",
}),
}),
expect.objectContaining({
name: "caps_get",
annotations: expect.objectContaining({
title: "Get a Cap recording",
}),
}),
expect.objectContaining({
name: "caps_context",
annotations: expect.objectContaining({
title: "Read a Cap recording",
}),
}),
]),
);
expect(listBody).not.toContain("caps_delete");
const called = await POST(
request({
Expand Down
1 change: 1 addition & 0 deletions apps/web/app/api/mcp/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const allowedOrigins = () =>
"https://chatgpt.com",
"https://claude.ai",
"https://muse.ai",
"https://grok.com",
]);

const corsHeaders = (request: Request) => {
Expand Down
4 changes: 3 additions & 1 deletion apps/web/lib/mcp-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export const isMcpRedirectUri = (value: string) => {
const url = new URL(value);
const loopback =
url.protocol === "http:" &&
(url.hostname === "127.0.0.1" || url.hostname === "[::1]") &&
(url.hostname === "localhost" ||
url.hostname === "127.0.0.1" ||
url.hostname === "[::1]") &&
url.port.length > 0;
return (
(url.protocol === "https:" || loopback) &&
Expand Down
7 changes: 4 additions & 3 deletions apps/web/lib/mcp-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const readOnly = {
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true,
openWorldHint: false,
};

const result = (value: Record<string, unknown>) => ({
Expand Down Expand Up @@ -54,7 +55,7 @@ export const createCapMcpServer = (userId: User.UserId) => {
},
additionalProperties: false,
}),
annotations: readOnly,
annotations: { ...readOnly, title: "List Cap recordings" },
},
async (input) => {
try {
Expand All @@ -77,7 +78,7 @@ export const createCapMcpServer = (userId: User.UserId) => {
required: ["id"],
additionalProperties: false,
}),
annotations: readOnly,
annotations: { ...readOnly, title: "Get a Cap recording" },
},
async ({ id }) => {
try {
Expand Down Expand Up @@ -105,7 +106,7 @@ export const createCapMcpServer = (userId: User.UserId) => {
required: ["id"],
additionalProperties: false,
}),
annotations: readOnly,
annotations: { ...readOnly, title: "Read a Cap recording" },
_meta: { ui: { resourceUri: cardUri }, "openai/outputTemplate": cardUri },
},
async ({ id, query }) => {
Expand Down
Loading