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>
This commit is contained in:
Jonathan Hefner 2026-01-23 09:51:29 -06:00
commit 6eb82b5562
No known key found for this signature in database
GPG key ID: 0DA40AFD21043691

View file

@ -23,19 +23,13 @@ import {
DEFAULT_PDF,
} from "./server.js";
export interface ServerOptions {
port: number;
name?: string;
}
/**
* Starts an MCP server with Streamable HTTP transport in stateless mode.
*/
export async function startServer(
export async function startStreamableHTTPServer(
createServer: () => McpServer,
options: ServerOptions,
): Promise<void> {
const { port, name = "MCP Server" } = options;
const port = parseInt(process.env.PORT ?? "3001", 10);
const app = createMcpExpressApp({ host: "0.0.0.0" });
app.use(cors());
@ -71,7 +65,7 @@ export async function startServer(
console.error("Failed to start server:", err);
process.exit(1);
}
console.log(`${name} listening on http://localhost:${port}/mcp`);
console.log(`MCP server listening on http://localhost:${port}/mcp`);
});
const shutdown = () => {
@ -83,6 +77,17 @@ export async function startServer(
process.on("SIGTERM", shutdown);
}
/**
* Starts an MCP server with stdio transport.
*
* @param createServer - Factory function that creates a new McpServer instance.
*/
export async function startStdioServer(
createServer: () => McpServer,
): Promise<void> {
await createServer().connect(new StdioServerTransport());
}
function parseArgs(): { urls: string[]; stdio: boolean } {
const args = process.argv.slice(2);
const urls: string[] = [];
@ -132,10 +137,9 @@ async function main() {
);
if (stdio) {
await createServer().connect(new StdioServerTransport());
await startStdioServer(createServer);
} else {
const port = parseInt(process.env.PORT ?? "3120", 10);
await startServer(createServer, { port, name: "PDF Server" });
await startStreamableHTTPServer(createServer);
}
}