Skip to content

Commit 2acc011

Browse files
chore(internal): refactor flag parsing for MCP servers and add debug flag
1 parent 801654c commit 2acc011

File tree

4 files changed

+43
-19
lines changed

4 files changed

+43
-19
lines changed

packages/mcp-server/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"fuse.js": "^7.1.0",
4141
"jq-web": "https://github.com/stainless-api/jq-web/releases/download/v0.8.8/jq-web.tar.gz",
4242
"morgan": "^1.10.0",
43+
"morgan-body": "^2.6.9",
4344
"qs": "^6.14.1",
4445
"typescript": "5.8.3",
4546
"yargs": "^17.7.2",

packages/mcp-server/src/http.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp';
44
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
55
import express from 'express';
66
import morgan from 'morgan';
7+
import morganBody from 'morgan-body';
78
import { McpOptions } from './options';
89
import { ClientOptions, initMcpServer, newMcpServer } from './server';
910
import { parseAuthHeaders } from './headers';
@@ -76,14 +77,26 @@ const del = async (req: express.Request, res: express.Response) => {
7677
export const streamableHTTPApp = ({
7778
clientOptions = {},
7879
mcpOptions,
80+
debug,
7981
}: {
8082
clientOptions?: ClientOptions;
8183
mcpOptions: McpOptions;
84+
debug: boolean;
8285
}): express.Express => {
8386
const app = express();
8487
app.set('query parser', 'extended');
8588
app.use(express.json());
86-
app.use(morgan('combined'));
89+
90+
if (debug) {
91+
morganBody(app, {
92+
logAllReqHeader: true,
93+
logAllResHeader: true,
94+
logRequestBody: true,
95+
logResponseBody: true,
96+
});
97+
} else {
98+
app.use(morgan('combined'));
99+
}
87100

88101
app.get('/', get);
89102
app.post('/', post({ clientOptions, mcpOptions }));
@@ -92,16 +105,20 @@ export const streamableHTTPApp = ({
92105
return app;
93106
};
94107

95-
export const launchStreamableHTTPServer = async (options: McpOptions, port: number | string | undefined) => {
96-
const app = streamableHTTPApp({ mcpOptions: options });
97-
const server = app.listen(port);
108+
export const launchStreamableHTTPServer = async (params: {
109+
mcpOptions: McpOptions;
110+
debug: boolean;
111+
port: number | string | undefined;
112+
}) => {
113+
const app = streamableHTTPApp({ mcpOptions: params.mcpOptions, debug: params.debug });
114+
const server = app.listen(params.port);
98115
const address = server.address();
99116

100117
if (typeof address === 'string') {
101118
console.error(`MCP Server running on streamable HTTP at ${address}`);
102119
} else if (address !== null) {
103120
console.error(`MCP Server running on streamable HTTP on port ${address.port}`);
104121
} else {
105-
console.error(`MCP Server running on streamable HTTP on port ${port}`);
122+
console.error(`MCP Server running on streamable HTTP on port ${params.port}`);
106123
}
107124
};

packages/mcp-server/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ async function main() {
2121
await launchStdioServer();
2222
break;
2323
case 'http':
24-
await launchStreamableHTTPServer(options, options.port ?? options.socket);
24+
await launchStreamableHTTPServer({
25+
mcpOptions: options,
26+
debug: options.debug,
27+
port: options.port ?? options.socket,
28+
});
2529
break;
2630
}
2731
}

packages/mcp-server/src/options.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { hideBin } from 'yargs/helpers';
44
import z from 'zod';
55

66
export type CLIOptions = McpOptions & {
7+
debug: boolean;
78
transport: 'stdio' | 'http';
89
port: number | undefined;
910
socket: string | undefined;
@@ -15,33 +16,33 @@ export type McpOptions = {
1516

1617
export function parseCLIOptions(): CLIOptions {
1718
const opts = yargs(hideBin(process.argv))
18-
.option('tools', {
19+
.option('debug', { type: 'boolean', description: 'Enable debug logging' })
20+
.option('no-tools', {
1921
type: 'string',
2022
array: true,
2123
choices: ['code', 'docs'],
22-
description: 'Use dynamic tools or all tools',
24+
description: 'Tools to explicitly disable',
2325
})
24-
.option('no-tools', {
26+
.option('port', {
27+
type: 'number',
28+
default: 3000,
29+
description: 'Port to serve on if using http transport',
30+
})
31+
.option('socket', { type: 'string', description: 'Unix socket to serve on if using http transport' })
32+
.option('tools', {
2533
type: 'string',
2634
array: true,
2735
choices: ['code', 'docs'],
28-
description: 'Do not use any dynamic or all tools',
36+
description: 'Tools to explicitly enable',
2937
})
3038
.option('transport', {
3139
type: 'string',
3240
choices: ['stdio', 'http'],
3341
default: 'stdio',
3442
description: 'What transport to use; stdio for local servers or http for remote servers',
3543
})
36-
.option('port', {
37-
type: 'number',
38-
default: 3000,
39-
description: 'Port to serve on if using http transport',
40-
})
41-
.option('socket', {
42-
type: 'string',
43-
description: 'Unix socket to serve on if using http transport',
44-
})
44+
.env('MCP_SERVER')
45+
.version(true)
4546
.help();
4647

4748
const argv = opts.parseSync();
@@ -57,6 +58,7 @@ export function parseCLIOptions(): CLIOptions {
5758

5859
return {
5960
...(includeDocsTools !== undefined && { includeDocsTools }),
61+
debug: !!argv.debug,
6062
transport,
6163
port: argv.port,
6264
socket: argv.socket,

0 commit comments

Comments
 (0)