-
Notifications
You must be signed in to change notification settings - Fork 49
Clean up examples #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ochafik
merged 4 commits into
modelcontextprotocol:main
from
jonathanhefner:refactor-examples
Dec 19, 2025
Merged
Clean up examples #182
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e8f7ec1
Centralize example server transport logic in shared utils
jonathanhefner 9fa2c2a
Make example servers self-contained with local server-utils
jonathanhefner 2df8bb1
Add `integration-server` example for E2E testing
jonathanhefner bf23882
Simplify `basic-server-*` examples
jonathanhefner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /** | ||
| * 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"; | ||
|
|
||
| /** | ||
| * Starts an MCP server using the appropriate transport based on command-line arguments. | ||
| * | ||
| * If `--stdio` is passed, uses stdio transport. Otherwise, uses Streamable HTTP transport. | ||
| * | ||
| * @param createServer - Factory function that creates a new McpServer instance. | ||
| */ | ||
| export async function startServer( | ||
| createServer: () => McpServer, | ||
| ): Promise<void> { | ||
| try { | ||
| if (process.argv.includes("--stdio")) { | ||
| await startStdioServer(createServer); | ||
| } else { | ||
| await startStreamableHttpServer(createServer); | ||
| } | ||
| } catch (e) { | ||
| console.error(e); | ||
| process.exit(1); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 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. | ||
| * | ||
| * Each request creates a fresh server and transport instance, which are | ||
| * closed when the response ends (no session tracking). | ||
| * | ||
| * The server listens on the port specified by the PORT environment variable, | ||
| * defaulting to 3001 if not set. | ||
| * | ||
| * @param createServer - Factory function that creates a new McpServer instance per request. | ||
| */ | ||
| export async function startStreamableHttpServer( | ||
| createServer: () => McpServer, | ||
| ): Promise<void> { | ||
| const port = parseInt(process.env.PORT ?? "3001", 10); | ||
|
|
||
| // Express app - bind to all interfaces for development/testing | ||
| const expressApp = createMcpExpressApp({ host: "0.0.0.0" }); | ||
| expressApp.use(cors()); | ||
|
|
||
| expressApp.all("/mcp", async (req: Request, res: Response) => { | ||
| // Create fresh server and transport for each request (stateless mode) | ||
| const server = createServer(); | ||
| const transport = new StreamableHTTPServerTransport({ | ||
| sessionIdGenerator: undefined, | ||
| }); | ||
|
|
||
| // Clean up when response ends | ||
| res.on("close", () => { | ||
| transport.close().catch(() => {}); | ||
| server.close().catch(() => {}); | ||
| }); | ||
|
|
||
| try { | ||
| await server.connect(transport); | ||
| await transport.handleRequest(req, res, req.body); | ||
| } catch (error) { | ||
| console.error("MCP error:", error); | ||
| if (!res.headersSent) { | ||
| res.status(500).json({ | ||
| jsonrpc: "2.0", | ||
| error: { code: -32603, message: "Internal server error" }, | ||
| id: null, | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| const { promise, resolve, reject } = Promise.withResolvers<void>(); | ||
|
|
||
| const httpServer = expressApp.listen(port, (err?: Error) => { | ||
| if (err) return reject(err); | ||
| console.log(`Server listening on http://localhost:${port}/mcp`); | ||
| resolve(); | ||
| }); | ||
|
|
||
| const shutdown = () => { | ||
| console.log("\nShutting down..."); | ||
| httpServer.close(() => process.exit(0)); | ||
| }; | ||
|
|
||
| process.on("SIGINT", shutdown); | ||
| process.on("SIGTERM", shutdown); | ||
|
|
||
| return promise; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note:
_meta: { ui: { resourceUri } }is the new cool