Skip to content

Commit f939116

Browse files
committed
Update server-everything to allow choosing the transport on the command line.
# Run the default (stdio) server ```npx @modelcontextprotocol/server-everything``` # Or specify stdio explicitly ```npx @modelcontextprotocol/server-everything stdio``` # Run the SSE server ```npx @modelcontextprotocol/server-everything sse``` # Run the streamable HTTP server ```npx @modelcontextprotocol/server-everything streamableHttp``` * In src/everything/index.ts - refactor/extracted contents to stdio.ts - replaced with code that - Gets the single argument from the commandline as scriptName - switches on scriptName - imports the appropriate server script or outputs usage options - scripts run on import * In src/everything/stdio.ts - added console log "Starting default (STDIO) server..." * In src/everything/sse.ts - added console log "Starting SSE server..." * In src/everything/streamableHttp.ts - added console log "Starting Streamable HTTP server..." * This fixes #1594
1 parent 0ad83e8 commit f939116

4 files changed

Lines changed: 62 additions & 18 deletions

File tree

‎src/everything/index.ts‎

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
11
#!/usr/bin/env node
22

3-
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
4-
import { createServer } from "./everything.js";
3+
// Parse command line arguments first
4+
const args = process.argv.slice(2);
5+
const scriptName = args[0] || 'stdio';
56

6-
async function main() {
7-
const transport = new StdioServerTransport();
8-
const { server, cleanup } = createServer();
9-
10-
await server.connect(transport);
11-
12-
// Cleanup on exit
13-
process.on("SIGINT", async () => {
14-
await cleanup();
15-
await server.close();
16-
process.exit(0);
17-
});
7+
async function run() {
8+
try {
9+
// Dynamically import only the requested module to prevent all modules from initializing
10+
switch (scriptName) {
11+
case 'stdio':
12+
// Import and run the default server
13+
await import('./stdio.js');
14+
break;
15+
case 'sse':
16+
// Import and run the SSE server
17+
await import('./sse.js');
18+
break;
19+
case 'streamableHttp':
20+
// Import and run the streamable HTTP server
21+
await import('./streamableHttp.js');
22+
break;
23+
default:
24+
console.error(`Unknown script: ${scriptName}`);
25+
console.log('Available scripts:');
26+
console.log('- stdio');
27+
console.log('- sse');
28+
console.log('- streamableHttp');
29+
process.exit(1);
30+
}
31+
} catch (error) {
32+
console.error('Error running script:', error);
33+
process.exit(1);
34+
}
1835
}
1936

20-
main().catch((error) => {
21-
console.error("Server error:", error);
22-
process.exit(1);
23-
});
37+
run();

‎src/everything/sse.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
22
import express from "express";
33
import { createServer } from "./everything.js";
44

5+
console.log('Starting SSE server...');
6+
57
const app = express();
68

79
const { server, cleanup } = createServer();

‎src/everything/stdio.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env node
2+
3+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
4+
import { createServer } from "./everything.js";
5+
6+
console.log('Starting default (STDIO) server...');
7+
8+
async function main() {
9+
const transport = new StdioServerTransport();
10+
const {server, cleanup} = createServer();
11+
12+
await server.connect(transport);
13+
14+
// Cleanup on exit
15+
process.on("SIGINT", async () => {
16+
await cleanup();
17+
await server.close();
18+
process.exit(0);
19+
});
20+
}
21+
22+
main().catch((error) => {
23+
console.error("Server error:", error);
24+
process.exit(1);
25+
});
26+

‎src/everything/streamableHttp.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import express, { Request, Response } from "express";
44
import { createServer } from "./everything.js";
55
import { randomUUID } from 'node:crypto';
66

7+
console.log('Starting Streamable HTTP server...');
8+
79
const app = express();
810

911
const { server, cleanup } = createServer();

0 commit comments

Comments
 (0)