From 94b8207a4d929f78b309bafb5ea9763cd8723956 Mon Sep 17 00:00:00 2001 From: viktormarinho Date: Thu, 23 Oct 2025 12:20:09 -0300 Subject: [PATCH] feat: add --transport flag and support for running the MCP server via HTTP --- package.json | 2 ++ src/cli.ts | 7 +++++++ src/main.ts | 39 +++++++++++++++++++++++++++++++++++---- src/third_party/index.ts | 1 + 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 8859732d..2f4e97df 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "@rollup/plugin-node-resolve": "^16.0.3", "@stylistic/eslint-plugin": "^5.4.0", "@types/debug": "^4.1.12", + "@types/express": "^5.0.3", "@types/filesystem": "^0.0.36", "@types/node": "^24.3.3", "@types/sinon": "^17.0.4", @@ -66,6 +67,7 @@ "sinon": "^21.0.0", "typescript": "^5.9.2", "typescript-eslint": "^8.43.0", + "express": "^5.1.0", "yargs": "18.0.0" }, "engines": { diff --git a/src/cli.ts b/src/cli.ts index 5e811ba2..49734cfb 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -154,6 +154,12 @@ export const cliOptions = { default: true, describe: 'Set to false to exlcude tools related to network.', }, + transport: { + type: 'string', + description: 'Transport to use for the MCP server. Available options are: stdio, http.', + choices: ['stdio', 'http'] as const, + default: 'stdio', + }, } satisfies Record; export function parseArguments(version: string, argv = process.argv) { @@ -206,6 +212,7 @@ export function parseArguments(version: string, argv = process.argv) { 'Disable tools in the performance category', ], ['$0 --no-category-network', 'Disable tools in the network category'], + ['$0 --transport http', 'Use HTTP transport for the MCP server'], ]); return yargsInstance diff --git a/src/main.ts b/src/main.ts index 6dd783fb..c58f9ab1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -15,6 +15,7 @@ import {McpResponse} from './McpResponse.js'; import {Mutex} from './Mutex.js'; import { McpServer, + StreamableHTTPServerTransport, StdioServerTransport, type CallToolResult, SetLevelRequestSchema, @@ -184,7 +185,37 @@ for (const tool of tools) { registerTool(tool); } -const transport = new StdioServerTransport(); -await server.connect(transport); -logger('Chrome DevTools MCP Server connected'); -logDisclaimers(); +if (args.transport === 'stdio') { + const transport = new StdioServerTransport(); + await server.connect(transport); + logger('Chrome DevTools MCP Server connected via stdio'); + logDisclaimers(); +} + +if (args.transport === 'http') { + const { default: express } = await import('express'); + const app = express(); + app.use(express.json()); + + app.all("/mcp", async (req, res) => { + const transport = new StreamableHTTPServerTransport({ + sessionIdGenerator: undefined, + enableJsonResponse: true, + }); + + res.on("close", () => { + transport.close(); + }); + + await server.connect(transport); + return transport.handleRequest(req, res, req.body); + }); + + app.listen(3000, () => { + logger('Chrome DevTools MCP Server listening on port 3000 via http'); + logDisclaimers(); + }).on("error", (error) => { + logger('Chrome DevTools MCP Server error:', error); + process.exit(1); + }); +} \ No newline at end of file diff --git a/src/third_party/index.ts b/src/third_party/index.ts index c9995d15..b170f57a 100644 --- a/src/third_party/index.ts +++ b/src/third_party/index.ts @@ -13,6 +13,7 @@ export {debug} from 'debug'; export type {Debugger} from 'debug'; export {McpServer} from '@modelcontextprotocol/sdk/server/mcp.js'; export {StdioServerTransport} from '@modelcontextprotocol/sdk/server/stdio.js'; +export {StreamableHTTPServerTransport} from '@modelcontextprotocol/sdk/server/streamableHttp.js'; export { type CallToolResult, SetLevelRequestSchema,