Skip to content

Commit b197935

Browse files
Refactor server startup functions
Rename `startServer` to `startStreamableHTTPServer` and add a new `startStdioServer` helper to make the transport type explicit. Remove the `ServerOptions` interface in favor of reading `PORT` directly from the environment inside the function (defaulting to `3001`). Also update Python examples to default to port `3001` for consistency. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 655b05e commit b197935

23 files changed

Lines changed: 338 additions & 352 deletions

File tree

examples/basic-server-preact/main.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
* Or: node dist/index.js [--stdio]
55
*/
66

7-
/**
8-
* Shared utilities for running MCP servers with Streamable HTTP transport.
9-
*/
10-
117
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
128
import { createMcpExpressApp } from "@modelcontextprotocol/sdk/server/express.js";
139
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
@@ -16,22 +12,15 @@ import cors from "cors";
1612
import type { Request, Response } from "express";
1713
import { createServer } from "./server.js";
1814

19-
export interface ServerOptions {
20-
port: number;
21-
name?: string;
22-
}
23-
2415
/**
2516
* Starts an MCP server with Streamable HTTP transport in stateless mode.
2617
*
2718
* @param createServer - Factory function that creates a new McpServer instance per request.
28-
* @param options - Server configuration options.
2919
*/
30-
export async function startServer(
20+
export async function startStreamableHTTPServer(
3121
createServer: () => McpServer,
32-
options: ServerOptions,
3322
): Promise<void> {
34-
const { port, name = "MCP Server" } = options;
23+
const port = parseInt(process.env.PORT ?? "3001", 10);
3524

3625
const app = createMcpExpressApp({ host: "0.0.0.0" });
3726
app.use(cors());
@@ -67,7 +56,7 @@ export async function startServer(
6756
console.error("Failed to start server:", err);
6857
process.exit(1);
6958
}
70-
console.log(`${name} listening on http://localhost:${port}/mcp`);
59+
console.log(`MCP server listening on http://localhost:${port}/mcp`);
7160
});
7261

7362
const shutdown = () => {
@@ -79,12 +68,22 @@ export async function startServer(
7968
process.on("SIGTERM", shutdown);
8069
}
8170

71+
/**
72+
* Starts an MCP server with stdio transport.
73+
*
74+
* @param createServer - Factory function that creates a new McpServer instance.
75+
*/
76+
export async function startStdioServer(
77+
createServer: () => McpServer,
78+
): Promise<void> {
79+
await createServer().connect(new StdioServerTransport());
80+
}
81+
8282
async function main() {
8383
if (process.argv.includes("--stdio")) {
84-
await createServer().connect(new StdioServerTransport());
84+
await startStdioServer(createServer);
8585
} else {
86-
const port = parseInt(process.env.PORT ?? "3001", 10);
87-
await startServer(createServer, { port, name: "Basic MCP App Server (Preact)" });
86+
await startStreamableHTTPServer(createServer);
8887
}
8988
}
9089

examples/basic-server-react/main.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,15 @@ import cors from "cors";
1212
import type { Request, Response } from "express";
1313
import { createServer } from "./server.js";
1414

15-
export interface ServerOptions {
16-
port: number;
17-
name?: string;
18-
}
19-
2015
/**
2116
* Starts an MCP server with Streamable HTTP transport in stateless mode.
2217
*
2318
* @param createServer - Factory function that creates a new McpServer instance per request.
24-
* @param options - Server configuration options.
2519
*/
26-
export async function startServer(
20+
export async function startStreamableHTTPServer(
2721
createServer: () => McpServer,
28-
options: ServerOptions,
2922
): Promise<void> {
30-
const { port, name = "MCP Server" } = options;
23+
const port = parseInt(process.env.PORT ?? "3001", 10);
3124

3225
const app = createMcpExpressApp({ host: "0.0.0.0" });
3326
app.use(cors());
@@ -63,7 +56,7 @@ export async function startServer(
6356
console.error("Failed to start server:", err);
6457
process.exit(1);
6558
}
66-
console.log(`${name} listening on http://localhost:${port}/mcp`);
59+
console.log(`MCP server listening on http://localhost:${port}/mcp`);
6760
});
6861

6962
const shutdown = () => {
@@ -75,12 +68,22 @@ export async function startServer(
7568
process.on("SIGTERM", shutdown);
7669
}
7770

71+
/**
72+
* Starts an MCP server with stdio transport.
73+
*
74+
* @param createServer - Factory function that creates a new McpServer instance.
75+
*/
76+
export async function startStdioServer(
77+
createServer: () => McpServer,
78+
): Promise<void> {
79+
await createServer().connect(new StdioServerTransport());
80+
}
81+
7882
async function main() {
7983
if (process.argv.includes("--stdio")) {
80-
await createServer().connect(new StdioServerTransport());
84+
await startStdioServer(createServer);
8185
} else {
82-
const port = parseInt(process.env.PORT ?? "3001", 10);
83-
await startServer(createServer, { port, name: "Basic MCP App Server (React)" });
86+
await startStreamableHTTPServer(createServer);
8487
}
8588
}
8689

examples/basic-server-solid/main.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
* Or: node dist/index.js [--stdio]
55
*/
66

7-
/**
8-
* Shared utilities for running MCP servers with Streamable HTTP transport.
9-
*/
10-
117
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
128
import { createMcpExpressApp } from "@modelcontextprotocol/sdk/server/express.js";
139
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
@@ -16,22 +12,15 @@ import cors from "cors";
1612
import type { Request, Response } from "express";
1713
import { createServer } from "./server.js";
1814

19-
export interface ServerOptions {
20-
port: number;
21-
name?: string;
22-
}
23-
2415
/**
2516
* Starts an MCP server with Streamable HTTP transport in stateless mode.
2617
*
2718
* @param createServer - Factory function that creates a new McpServer instance per request.
28-
* @param options - Server configuration options.
2919
*/
30-
export async function startServer(
20+
export async function startStreamableHTTPServer(
3121
createServer: () => McpServer,
32-
options: ServerOptions,
3322
): Promise<void> {
34-
const { port, name = "MCP Server" } = options;
23+
const port = parseInt(process.env.PORT ?? "3001", 10);
3524

3625
const app = createMcpExpressApp({ host: "0.0.0.0" });
3726
app.use(cors());
@@ -67,7 +56,7 @@ export async function startServer(
6756
console.error("Failed to start server:", err);
6857
process.exit(1);
6958
}
70-
console.log(`${name} listening on http://localhost:${port}/mcp`);
59+
console.log(`MCP server listening on http://localhost:${port}/mcp`);
7160
});
7261

7362
const shutdown = () => {
@@ -79,12 +68,22 @@ export async function startServer(
7968
process.on("SIGTERM", shutdown);
8069
}
8170

71+
/**
72+
* Starts an MCP server with stdio transport.
73+
*
74+
* @param createServer - Factory function that creates a new McpServer instance.
75+
*/
76+
export async function startStdioServer(
77+
createServer: () => McpServer,
78+
): Promise<void> {
79+
await createServer().connect(new StdioServerTransport());
80+
}
81+
8282
async function main() {
8383
if (process.argv.includes("--stdio")) {
84-
await createServer().connect(new StdioServerTransport());
84+
await startStdioServer(createServer);
8585
} else {
86-
const port = parseInt(process.env.PORT ?? "3001", 10);
87-
await startServer(createServer, { port, name: "Basic MCP App Server (Solid)" });
86+
await startStreamableHTTPServer(createServer);
8887
}
8988
}
9089

examples/basic-server-svelte/main.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
* Or: node dist/index.js [--stdio]
55
*/
66

7-
/**
8-
* Shared utilities for running MCP servers with Streamable HTTP transport.
9-
*/
10-
117
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
128
import { createMcpExpressApp } from "@modelcontextprotocol/sdk/server/express.js";
139
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
@@ -16,22 +12,15 @@ import cors from "cors";
1612
import type { Request, Response } from "express";
1713
import { createServer } from "./server.js";
1814

19-
export interface ServerOptions {
20-
port: number;
21-
name?: string;
22-
}
23-
2415
/**
2516
* Starts an MCP server with Streamable HTTP transport in stateless mode.
2617
*
2718
* @param createServer - Factory function that creates a new McpServer instance per request.
28-
* @param options - Server configuration options.
2919
*/
30-
export async function startServer(
20+
export async function startStreamableHTTPServer(
3121
createServer: () => McpServer,
32-
options: ServerOptions,
3322
): Promise<void> {
34-
const { port, name = "MCP Server" } = options;
23+
const port = parseInt(process.env.PORT ?? "3001", 10);
3524

3625
const app = createMcpExpressApp({ host: "0.0.0.0" });
3726
app.use(cors());
@@ -67,7 +56,7 @@ export async function startServer(
6756
console.error("Failed to start server:", err);
6857
process.exit(1);
6958
}
70-
console.log(`${name} listening on http://localhost:${port}/mcp`);
59+
console.log(`MCP server listening on http://localhost:${port}/mcp`);
7160
});
7261

7362
const shutdown = () => {
@@ -79,12 +68,22 @@ export async function startServer(
7968
process.on("SIGTERM", shutdown);
8069
}
8170

71+
/**
72+
* Starts an MCP server with stdio transport.
73+
*
74+
* @param createServer - Factory function that creates a new McpServer instance.
75+
*/
76+
export async function startStdioServer(
77+
createServer: () => McpServer,
78+
): Promise<void> {
79+
await createServer().connect(new StdioServerTransport());
80+
}
81+
8282
async function main() {
8383
if (process.argv.includes("--stdio")) {
84-
await createServer().connect(new StdioServerTransport());
84+
await startStdioServer(createServer);
8585
} else {
86-
const port = parseInt(process.env.PORT ?? "3001", 10);
87-
await startServer(createServer, { port, name: "Basic MCP App Server (Svelte)" });
86+
await startStreamableHTTPServer(createServer);
8887
}
8988
}
9089

examples/basic-server-vanillajs/main.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
* Or: node dist/index.js [--stdio]
55
*/
66

7-
/**
8-
* Shared utilities for running MCP servers with Streamable HTTP transport.
9-
*/
10-
117
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
128
import { createMcpExpressApp } from "@modelcontextprotocol/sdk/server/express.js";
139
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
@@ -16,22 +12,15 @@ import cors from "cors";
1612
import type { Request, Response } from "express";
1713
import { createServer } from "./server.js";
1814

19-
export interface ServerOptions {
20-
port: number;
21-
name?: string;
22-
}
23-
2415
/**
2516
* Starts an MCP server with Streamable HTTP transport in stateless mode.
2617
*
2718
* @param createServer - Factory function that creates a new McpServer instance per request.
28-
* @param options - Server configuration options.
2919
*/
30-
export async function startServer(
20+
export async function startStreamableHTTPServer(
3121
createServer: () => McpServer,
32-
options: ServerOptions,
3322
): Promise<void> {
34-
const { port, name = "MCP Server" } = options;
23+
const port = parseInt(process.env.PORT ?? "3001", 10);
3524

3625
const app = createMcpExpressApp({ host: "0.0.0.0" });
3726
app.use(cors());
@@ -67,7 +56,7 @@ export async function startServer(
6756
console.error("Failed to start server:", err);
6857
process.exit(1);
6958
}
70-
console.log(`${name} listening on http://localhost:${port}/mcp`);
59+
console.log(`MCP server listening on http://localhost:${port}/mcp`);
7160
});
7261

7362
const shutdown = () => {
@@ -79,12 +68,22 @@ export async function startServer(
7968
process.on("SIGTERM", shutdown);
8069
}
8170

71+
/**
72+
* Starts an MCP server with stdio transport.
73+
*
74+
* @param createServer - Factory function that creates a new McpServer instance.
75+
*/
76+
export async function startStdioServer(
77+
createServer: () => McpServer,
78+
): Promise<void> {
79+
await createServer().connect(new StdioServerTransport());
80+
}
81+
8282
async function main() {
8383
if (process.argv.includes("--stdio")) {
84-
await createServer().connect(new StdioServerTransport());
84+
await startStdioServer(createServer);
8585
} else {
86-
const port = parseInt(process.env.PORT ?? "3102", 10);
87-
await startServer(createServer, { port, name: "Basic MCP App Server (Vanilla JS)" });
86+
await startStreamableHTTPServer(createServer);
8887
}
8988
}
9089

0 commit comments

Comments
 (0)