forked from oomol-lab/open-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutors.test.ts
More file actions
78 lines (69 loc) · 2.84 KB
/
Copy pathexecutors.test.ts
File metadata and controls
78 lines (69 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { describe, expect, it, vi } from "vitest";
import { cloudflareMcpActionHandlers, credentialValidators } from "./executors.ts";
describe("Cloudflare MCP executors", () => {
it("sends bearer auth and maps search to the official MCP tool", async () => {
const calls: Array<Record<string, unknown>> = [];
const fetcher = createMcpFetch(calls);
const output = await cloudflareMcpActionHandlers.search(
{ code: "async () => ['workers']" },
{ accessToken: "cf-token", fetcher },
);
expect(output).toEqual(["workers"]);
expect(calls.find((call) => call.method === "tools/call")?.params).toEqual({
name: "search",
arguments: { code: "async () => ['workers']" },
});
expect(fetcher).toHaveBeenCalled();
});
it("validates both API-token and OAuth bearer credentials with tools/list", async () => {
const apiKeyResult = await credentialValidators.apiKey!(
{ apiKey: "api-token", values: {} },
{ fetcher: createMcpFetch([]) },
);
const oauthResult = await credentialValidators.oauth2!(
{
authType: "oauth2",
accessToken: "oauth-token",
tokenType: "Bearer",
profile: { accountId: "pending", displayName: "Pending", grantedScopes: [] },
metadata: {},
},
{ fetcher: createMcpFetch([]) },
);
expect(apiKeyResult?.metadata?.mcpTools).toEqual(["docs", "execute", "search"]);
expect(oauthResult?.metadata?.mcpTools).toEqual(["docs", "execute", "search"]);
});
});
function createMcpFetch(calls: Array<Record<string, unknown>>): typeof fetch {
return vi.fn(async (_input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
const headers = new Headers(init?.headers);
const rawBody = typeof init?.body === "string" ? init.body : undefined;
const request = rawBody ? (JSON.parse(rawBody) as Record<string, unknown>) : {};
calls.push(request);
expect(headers.get("authorization")).toMatch(/^Bearer /);
if (init?.method === "DELETE") return new Response(null, { status: 200 });
if (!("id" in request)) return new Response(null, { status: 202 });
const method = request.method;
const result =
method === "initialize"
? {
protocolVersion: "2025-03-26",
capabilities: {},
serverInfo: { name: "cloudflare-api", version: "1.0.0" },
}
: method === "tools/list"
? {
tools: ["docs", "execute", "search"].map((name) => ({
name,
inputSchema: { type: "object" },
})),
}
: { content: [{ type: "text", text: '["workers"]' }] };
return new Response(JSON.stringify({ jsonrpc: "2.0", id: request.id, result }), {
headers: {
"content-type": "application/json",
"mcp-session-id": "test-session",
},
});
}) as typeof fetch;
}