Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions examples/basic-server-preact/server-utils.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
/**
* Shared utilities for running MCP servers with Streamable HTTP transport.
* Shared utilities for running MCP servers with various transports.
*/

import { createMcpExpressApp } from "@modelcontextprotocol/sdk/server/express.js";
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
import cors from "cors";
import type { Request, Response } from "express";

export interface ServerOptions {
port: number;
name?: string;
/**
* 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());
}

/**
* Starts an MCP server with Streamable HTTP transport in stateless mode.
*
* @param createServer - Factory function that creates a new McpServer instance per request.
* @param options - Server configuration options.
*/
export async function startServer(
export async function startHttpServer(
createServer: () => McpServer,
options: ServerOptions,
): Promise<void> {
const { port, name = "MCP Server" } = options;
const port = parseInt(process.env.PORT ?? "3001", 10);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This introduces process.env again in server-utils, I don't like this direction tbh

And the startStdioServer method is a lot of lines to replace a one-liner.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This introduces process.env again in server-utils, I don't like this direction tbh

The PORT env variable is very widely used, so reading from wherever in the code base is pretty typical for web servers.

And the startStdioServer method is a lot of lines to replace a one-liner.

The intent is instant clarity. Compare:

  if (process.argv.includes("--stdio")) {
    await createServer().connect(new StdioServerTransport());
  } else {
    const port = parseInt(process.env.PORT ?? "3001", 10);
    await startServer(createServer, { port, name: "Basic MCP App Server (Preact)" });
  }

vs

  process.argv.includes("--stdio")
    ? await startStdioServer(createServer)
    : await startHttpServer(createServer);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmh fair enough won't die on this hill, thanks! ;-)


const app = createMcpExpressApp({ host: "0.0.0.0" });
app.use(cors());
Expand Down Expand Up @@ -55,7 +60,7 @@ export async function startServer(
});

const httpServer = app.listen(port, () => {
console.log(`${name} listening on http://localhost:${port}/mcp`);
console.log(`Server listening on http://localhost:${port}/mcp`);
});

const shutdown = () => {
Expand Down
12 changes: 4 additions & 8 deletions examples/basic-server-preact/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import type { CallToolResult, ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";
import fs from "node:fs/promises";
import path from "node:path";
import { registerAppTool, registerAppResource, RESOURCE_MIME_TYPE } from "@modelcontextprotocol/ext-apps/server";
import { startServer } from "./server-utils.js";
import { startStdioServer, startHttpServer } from "./server-utils.js";

const DIST_DIR = path.join(import.meta.dirname, "dist");

Expand Down Expand Up @@ -57,12 +56,9 @@ function createServer(): McpServer {
}

async function main() {
if (process.argv.includes("--stdio")) {
await createServer().connect(new StdioServerTransport());
} else {
const port = parseInt(process.env.PORT ?? "3001", 10);
await startServer(createServer, { port, name: "Basic MCP App Server (Preact)" });
}
process.argv.includes("--stdio")
? await startStdioServer(createServer)
: await startHttpServer(createServer);
}

main().catch((e) => {
Expand Down
23 changes: 14 additions & 9 deletions examples/basic-server-react/server-utils.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
/**
* Shared utilities for running MCP servers with Streamable HTTP transport.
* Shared utilities for running MCP servers with various transports.
*/

import { createMcpExpressApp } from "@modelcontextprotocol/sdk/server/express.js";
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
import cors from "cors";
import type { Request, Response } from "express";

export interface ServerOptions {
port: number;
name?: string;
/**
* 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());
}

/**
* Starts an MCP server with Streamable HTTP transport in stateless mode.
*
* @param createServer - Factory function that creates a new McpServer instance per request.
* @param options - Server configuration options.
*/
export async function startServer(
export async function startHttpServer(
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());
Expand Down Expand Up @@ -55,7 +60,7 @@ export async function startServer(
});

const httpServer = app.listen(port, () => {
console.log(`${name} listening on http://localhost:${port}/mcp`);
console.log(`Server listening on http://localhost:${port}/mcp`);
});

const shutdown = () => {
Expand Down
12 changes: 4 additions & 8 deletions examples/basic-server-react/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import type { CallToolResult, ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";
import fs from "node:fs/promises";
import path from "node:path";
import { registerAppTool, registerAppResource, RESOURCE_MIME_TYPE, RESOURCE_URI_META_KEY } from "@modelcontextprotocol/ext-apps/server";
import { startServer } from "./server-utils.js";
import { startStdioServer, startHttpServer } from "./server-utils.js";

const DIST_DIR = path.join(import.meta.dirname, "dist");

Expand Down Expand Up @@ -48,12 +47,9 @@ export function createServer(): McpServer {
}

async function main() {
if (process.argv.includes("--stdio")) {
await createServer().connect(new StdioServerTransport());
} else {
const port = parseInt(process.env.PORT ?? "3001", 10);
await startServer(createServer, { port, name: "Basic MCP App Server (React)" });
}
process.argv.includes("--stdio")
? await startStdioServer(createServer)
: await startHttpServer(createServer);
}

main().catch((e) => {
Expand Down
23 changes: 14 additions & 9 deletions examples/basic-server-solid/server-utils.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
/**
* Shared utilities for running MCP servers with Streamable HTTP transport.
* Shared utilities for running MCP servers with various transports.
*/

import { createMcpExpressApp } from "@modelcontextprotocol/sdk/server/express.js";
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
import cors from "cors";
import type { Request, Response } from "express";

export interface ServerOptions {
port: number;
name?: string;
/**
* 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());
}

/**
* Starts an MCP server with Streamable HTTP transport in stateless mode.
*
* @param createServer - Factory function that creates a new McpServer instance per request.
* @param options - Server configuration options.
*/
export async function startServer(
export async function startHttpServer(
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());
Expand Down Expand Up @@ -55,7 +60,7 @@ export async function startServer(
});

const httpServer = app.listen(port, () => {
console.log(`${name} listening on http://localhost:${port}/mcp`);
console.log(`Server listening on http://localhost:${port}/mcp`);
});

const shutdown = () => {
Expand Down
12 changes: 4 additions & 8 deletions examples/basic-server-solid/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import type { CallToolResult, ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";
import fs from "node:fs/promises";
import path from "node:path";
import { registerAppTool, registerAppResource, RESOURCE_MIME_TYPE } from "@modelcontextprotocol/ext-apps/server";
import { startServer } from "./server-utils.js";
import { startStdioServer, startHttpServer } from "./server-utils.js";

const DIST_DIR = path.join(import.meta.dirname, "dist");

Expand Down Expand Up @@ -57,12 +56,9 @@ function createServer(): McpServer {
}

async function main() {
if (process.argv.includes("--stdio")) {
await createServer().connect(new StdioServerTransport());
} else {
const port = parseInt(process.env.PORT ?? "3001", 10);
await startServer(createServer, { port, name: "Basic MCP App Server (Solid)" });
}
process.argv.includes("--stdio")
? await startStdioServer(createServer)
: await startHttpServer(createServer);
}

main().catch((e) => {
Expand Down
23 changes: 14 additions & 9 deletions examples/basic-server-svelte/server-utils.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
/**
* Shared utilities for running MCP servers with Streamable HTTP transport.
* Shared utilities for running MCP servers with various transports.
*/

import { createMcpExpressApp } from "@modelcontextprotocol/sdk/server/express.js";
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
import cors from "cors";
import type { Request, Response } from "express";

export interface ServerOptions {
port: number;
name?: string;
/**
* 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());
}

/**
* Starts an MCP server with Streamable HTTP transport in stateless mode.
*
* @param createServer - Factory function that creates a new McpServer instance per request.
* @param options - Server configuration options.
*/
export async function startServer(
export async function startHttpServer(
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());
Expand Down Expand Up @@ -55,7 +60,7 @@ export async function startServer(
});

const httpServer = app.listen(port, () => {
console.log(`${name} listening on http://localhost:${port}/mcp`);
console.log(`Server listening on http://localhost:${port}/mcp`);
});

const shutdown = () => {
Expand Down
12 changes: 4 additions & 8 deletions examples/basic-server-svelte/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import type { CallToolResult, ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";
import fs from "node:fs/promises";
import path from "node:path";
import { registerAppTool, registerAppResource, RESOURCE_MIME_TYPE } from "@modelcontextprotocol/ext-apps/server";
import { startServer } from "./server-utils.js";
import { startStdioServer, startHttpServer } from "./server-utils.js";

const DIST_DIR = path.join(import.meta.dirname, "dist");

Expand Down Expand Up @@ -57,12 +56,9 @@ function createServer(): McpServer {
}

async function main() {
if (process.argv.includes("--stdio")) {
await createServer().connect(new StdioServerTransport());
} else {
const port = parseInt(process.env.PORT ?? "3001", 10);
await startServer(createServer, { port, name: "Basic MCP App Server (Svelte)" });
}
process.argv.includes("--stdio")
? await startStdioServer(createServer)
: await startHttpServer(createServer);
}

main().catch((e) => {
Expand Down
23 changes: 14 additions & 9 deletions examples/basic-server-vanillajs/server-utils.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
/**
* Shared utilities for running MCP servers with Streamable HTTP transport.
* Shared utilities for running MCP servers with various transports.
*/

import { createMcpExpressApp } from "@modelcontextprotocol/sdk/server/express.js";
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
import cors from "cors";
import type { Request, Response } from "express";

export interface ServerOptions {
port: number;
name?: string;
/**
* 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());
}

/**
* Starts an MCP server with Streamable HTTP transport in stateless mode.
*
* @param createServer - Factory function that creates a new McpServer instance per request.
* @param options - Server configuration options.
*/
export async function startServer(
export async function startHttpServer(
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());
Expand Down Expand Up @@ -55,7 +60,7 @@ export async function startServer(
});

const httpServer = app.listen(port, () => {
console.log(`${name} listening on http://localhost:${port}/mcp`);
console.log(`Server listening on http://localhost:${port}/mcp`);
});

const shutdown = () => {
Expand Down
12 changes: 4 additions & 8 deletions examples/basic-server-vanillajs/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import type { CallToolResult, ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";
import fs from "node:fs/promises";
import path from "node:path";
import { registerAppTool, registerAppResource, RESOURCE_MIME_TYPE, RESOURCE_URI_META_KEY } from "@modelcontextprotocol/ext-apps/server";
import { startServer } from "./server-utils.js";
import { startStdioServer, startHttpServer } from "./server-utils.js";

const DIST_DIR = path.join(import.meta.dirname, "dist");
const RESOURCE_URI = "ui://get-time/mcp-app.html";
Expand Down Expand Up @@ -59,12 +58,9 @@ export function createServer(): McpServer {
}

async function main() {
if (process.argv.includes("--stdio")) {
await createServer().connect(new StdioServerTransport());
} else {
const port = parseInt(process.env.PORT ?? "3102", 10);
await startServer(createServer, { port, name: "Basic MCP App Server (Vanilla JS)" });
}
process.argv.includes("--stdio")
? await startStdioServer(createServer)
: await startHttpServer(createServer);
}

main().catch((e) => {
Expand Down
Loading
Loading