|
| 1 | +import { Server } from "@modelcontextprotocol/sdk/server/index.js"; |
| 2 | +import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 3 | +import { McpServer as McpServerNoSuffix } from "@modelcontextprotocol/sdk/server/mcp"; |
| 4 | +import { CallToolRequestSchema } from "@modelcontextprotocol/sdk/types.js"; |
| 5 | +import { McpServer as McpServerV2, Server as ServerV2 } from "@modelcontextprotocol/server"; |
| 6 | +import { z } from "zod"; |
| 7 | + |
| 8 | +declare function SINK(value: unknown): void; |
| 9 | + |
| 10 | +const server = new McpServer({ name: "test", version: "1.0.0" }); |
| 11 | + |
| 12 | +// Tools and prompts: the first parameter of the callback holds the arguments. |
| 13 | +server.tool("run", { command: z.string() }, async ({ command }) => { // $ threat-source=remote |
| 14 | + SINK(command); // $ hasFlow |
| 15 | + return { content: [] }; |
| 16 | +}); |
| 17 | + |
| 18 | +server.tool("read", "Read a file", { path: z.string() }, async (args) => { // $ threat-source=remote |
| 19 | + SINK(args.path); // $ hasFlow |
| 20 | + return { content: [] }; |
| 21 | +}); |
| 22 | + |
| 23 | +server.registerTool("fetch", { inputSchema: { url: z.string() } }, async ({ url }) => { // $ threat-source=remote |
| 24 | + SINK(url); // $ hasFlow |
| 25 | + return { content: [] }; |
| 26 | +}); |
| 27 | + |
| 28 | +async function namedHandler({ target }: { target: string }) { // $ threat-source=remote |
| 29 | + SINK(target); // $ hasFlow |
| 30 | + return { content: [] }; |
| 31 | +} |
| 32 | +server.tool("remove", { target: z.string() }, namedHandler); |
| 33 | + |
| 34 | +server.prompt("summarize", { topic: z.string() }, ({ topic }) => { // $ threat-source=remote |
| 35 | + SINK(topic); // $ hasFlow |
| 36 | + return { messages: [] }; |
| 37 | +}); |
| 38 | + |
| 39 | +server.registerPrompt("explain", { argsSchema: { file: z.string() } }, ({ file }) => { // $ threat-source=remote |
| 40 | + SINK(file); // $ hasFlow |
| 41 | + return { messages: [] }; |
| 42 | +}); |
| 43 | + |
| 44 | +// Resources: the URI and the variables of the template. |
| 45 | +server.resource("note", new ResourceTemplate("note://{name}", { list: undefined }), async (uri, { name }) => { // $ threat-source=remote |
| 46 | + SINK(uri.href); // $ hasFlow |
| 47 | + SINK(name); // $ hasFlow |
| 48 | + return { contents: [] }; |
| 49 | +}); |
| 50 | + |
| 51 | +server.registerResource("page", new ResourceTemplate("page://{host}", { list: undefined }), {}, async (uri, variables) => { // $ threat-source=remote |
| 52 | + SINK(variables.host); // $ hasFlow |
| 53 | + return { contents: [] }; |
| 54 | +}); |
| 55 | + |
| 56 | +// The request headers and the bearer token that the SDK hands to a callback. |
| 57 | +server.tool("forward", { id: z.string() }, async (_args, extra) => { // $ threat-source=remote |
| 58 | + SINK(extra.requestInfo?.headers["x-target"]); // $ hasFlow threat-source=remote |
| 59 | + SINK(extra.authInfo?.token); // $ hasFlow threat-source=remote |
| 60 | + SINK(extra.sessionId); |
| 61 | + return { content: [] }; |
| 62 | +}); |
| 63 | + |
| 64 | +// The low-level server: handlers receive the request. |
| 65 | +const lowLevel = new Server({ name: "test", version: "1.0.0" }, { capabilities: { tools: {} } }); |
| 66 | +lowLevel.setRequestHandler(CallToolRequestSchema, async (request) => { |
| 67 | + SINK(request.params.arguments?.command); // $ hasFlow threat-source=remote |
| 68 | + return { content: [] }; |
| 69 | +}); |
| 70 | + |
| 71 | +server.server.setRequestHandler(CallToolRequestSchema, async (request) => { |
| 72 | + SINK(request.params.name); // $ hasFlow threat-source=remote |
| 73 | + return { content: [] }; |
| 74 | +}); |
| 75 | + |
| 76 | +// An import path without the file suffix, and a server that arrives as a typed parameter. |
| 77 | +new McpServerNoSuffix({ name: "test", version: "1.0.0" }).tool("run", { command: z.string() }, async ({ command }) => { // $ threat-source=remote |
| 78 | + SINK(command); // $ hasFlow |
| 79 | + return { content: [] }; |
| 80 | +}); |
| 81 | + |
| 82 | +export function register(target: McpServer) { |
| 83 | + target.tool("run", { command: z.string() }, async ({ command }) => { // $ threat-source=remote |
| 84 | + SINK(command); // $ hasFlow |
| 85 | + return { content: [] }; |
| 86 | + }); |
| 87 | +} |
| 88 | + |
| 89 | +// Version 2 of the SDK: one package, and a context object instead of `extra`. |
| 90 | +const serverV2 = new McpServerV2({ name: "test", version: "2.0.0" }); |
| 91 | +serverV2.registerTool("fetch", { inputSchema: z.object({ url: z.string() }) }, async ({ url }, ctx) => { // $ threat-source=remote |
| 92 | + SINK(url); // $ hasFlow |
| 93 | + SINK(ctx.http?.req.headers.get("x-target")); // $ hasFlow threat-source=remote |
| 94 | + SINK(ctx.http?.authInfo?.token); // $ hasFlow threat-source=remote |
| 95 | + return { content: [] }; |
| 96 | +}); |
| 97 | + |
| 98 | +const lowLevelV2 = new ServerV2({ name: "test", version: "2.0.0" }); |
| 99 | +lowLevelV2.setRequestHandler("tools/call", async (request) => { |
| 100 | + SINK(request.params.arguments); // $ hasFlow threat-source=remote |
| 101 | + return { content: [] }; |
| 102 | +}); |
| 103 | + |
| 104 | +lowLevelV2.setRequestHandler("acme/search", { params: z.object({ query: z.string() }), result: z.object({}) }, async (params) => { // $ threat-source=remote |
| 105 | + SINK(params.query); // $ hasFlow |
| 106 | + return {}; |
| 107 | +}); |
| 108 | + |
| 109 | +// Not registered with a server: no source. |
| 110 | +export function notAHandler(command: string) { |
| 111 | + SINK(command); |
| 112 | +} |
0 commit comments