Skip to content

Commit 2645e03

Browse files
authored
Merge pull request #1673 from cliffhall/choose-server-at-startup-from-npx
Choose the server-everything transport on the command line
2 parents 1cdbeb3 + e30f308 commit 2645e03

File tree

5 files changed

+91
-20
lines changed

5 files changed

+91
-20
lines changed

src/everything/README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,45 @@ Optionally, you can add it to a file called `.vscode/mcp.json` in your workspace
173173
}
174174
```
175175

176-
## Run with [HTTP+SSE Transport](https://modelcontextprotocol.io/specification/2024-11-05/basic/transports#http-with-sse) (deprecated as of [2025-03-26](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports))
176+
## Running from source with [HTTP+SSE Transport](https://modelcontextprotocol.io/specification/2024-11-05/basic/transports#http-with-sse) (deprecated as of [2025-03-26](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports))
177177

178178
```shell
179179
cd src/everything
180180
npm install
181181
npm run start:sse
182182
```
183183

184-
## Run with [Streamable HTTP Transport](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#streamable-http)
184+
## Run from source with [Streamable HTTP Transport](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#streamable-http)
185185

186186
```shell
187187
cd src/everything
188188
npm install
189189
npm run start:streamableHttp
190190
```
191+
192+
## Running as an installed package
193+
### Install
194+
```shell
195+
npm install -g @modelcontextprotocol/server-everything@latest
196+
````
197+
198+
### Run the default (stdio) server
199+
```shell
200+
npx @modelcontextprotocol/server-everything
201+
```
202+
203+
### Or specify stdio explicitly
204+
```shell
205+
npx @modelcontextprotocol/server-everything stdio
206+
```
207+
208+
### Run the SSE server
209+
```shell
210+
npx @modelcontextprotocol/server-everything sse
211+
```
212+
213+
### Run the streamable HTTP server
214+
```shell
215+
npx @modelcontextprotocol/server-everything streamableHttp
216+
```
217+

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.error('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.error('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.error('Starting Streamable HTTP server...');
8+
79
const app = express();
810

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

0 commit comments

Comments
 (0)