-
Notifications
You must be signed in to change notification settings - Fork 47
feat: publish example servers to npm #184
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
Open
jerome3o-anthropic
wants to merge
6
commits into
main
Choose a base branch
from
jerome/publish-examples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,687
−2,601
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1efd0f9
feat: publish example servers to npm
jerome3o-anthropic a7ade06
feat: publish examples with pkg-pr-new for PR testing
jerome3o-anthropic c06ffb0
feat: export createServer from example servers
jerome3o-anthropic bff46cc
Merge origin/main into jerome/publish-examples
ochafik 771ff96
style: format integration-server/server.ts
ochafik 68cddec
fix: update e2e snapshots for basic-server examples
ochafik 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
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,68 @@ | ||
| /** | ||
| * Shared utilities for running MCP servers with Streamable HTTP transport. | ||
| */ | ||
|
|
||
| import { createMcpExpressApp } from "@modelcontextprotocol/sdk/server/express.js"; | ||
| import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.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 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( | ||
| createServer: () => McpServer, | ||
| options: ServerOptions, | ||
| ): Promise<void> { | ||
| const { port, name = "MCP Server" } = options; | ||
|
|
||
| const app = createMcpExpressApp({ host: "0.0.0.0" }); | ||
| app.use(cors()); | ||
|
|
||
| app.all("/mcp", async (req: Request, res: Response) => { | ||
| const server = createServer(); | ||
| const transport = new StreamableHTTPServerTransport({ | ||
| sessionIdGenerator: undefined, | ||
| }); | ||
|
|
||
| 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 httpServer = app.listen(port, () => { | ||
| console.log(`${name} listening on http://localhost:${port}/mcp`); | ||
| }); | ||
|
|
||
| const shutdown = () => { | ||
| console.log("\nShutting down..."); | ||
| httpServer.close(() => process.exit(0)); | ||
| }; | ||
|
|
||
| process.on("SIGINT", shutdown); | ||
| process.on("SIGTERM", shutdown); | ||
| } |
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 |
|---|---|---|
| @@ -1,58 +1,62 @@ | ||
| 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 "./src/server-utils.js"; | ||
| import { startServer } from "./server-utils.js"; | ||
|
|
||
| const DIST_DIR = path.join(import.meta.dirname, "dist"); | ||
|
|
||
| /** | ||
| * Creates a new MCP server instance with tools and resources registered. | ||
| */ | ||
| function createServer(): McpServer { | ||
| export function createServer(): McpServer { | ||
| const server = new McpServer({ | ||
| name: "Basic MCP App Server (React)", | ||
| version: "1.0.0", | ||
| }); | ||
|
|
||
| // Two-part registration: tool + resource, tied together by the resource URI. | ||
| const resourceUri = "ui://get-time/mcp-app.html"; | ||
|
|
||
| // Register a tool with UI metadata. When the host calls this tool, it reads | ||
| // `_meta[RESOURCE_URI_META_KEY]` to know which resource to fetch and render | ||
| // as an interactive UI. | ||
| registerAppTool(server, | ||
| "get-time", | ||
| { | ||
| title: "Get Time", | ||
| description: "Returns the current server time as an ISO 8601 string.", | ||
| description: "Returns the current server time.", | ||
| inputSchema: {}, | ||
| _meta: { [RESOURCE_URI_META_KEY]: resourceUri }, | ||
| }, | ||
| async (): Promise<CallToolResult> => { | ||
| const time = new Date().toISOString(); | ||
| return { content: [{ type: "text", text: time }] }; | ||
| return { content: [{ type: "text", text: new Date().toISOString() }] }; | ||
| }, | ||
| ); | ||
|
|
||
| // Register the resource, which returns the bundled HTML/JavaScript for the UI. | ||
| registerAppResource(server, | ||
| resourceUri, | ||
| resourceUri, | ||
| { mimeType: RESOURCE_MIME_TYPE }, | ||
| async (): Promise<ReadResourceResult> => { | ||
| const html = await fs.readFile(path.join(DIST_DIR, "mcp-app.html"), "utf-8"); | ||
|
|
||
| return { | ||
| contents: [ | ||
| { uri: resourceUri, mimeType: RESOURCE_MIME_TYPE, text: html }, | ||
| ], | ||
| contents: [{ uri: resourceUri, mimeType: RESOURCE_MIME_TYPE, text: html }], | ||
| }; | ||
| }, | ||
| ); | ||
|
|
||
| return server; | ||
| } | ||
|
|
||
| startServer(createServer); | ||
| 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)" }); | ||
| } | ||
| } | ||
|
|
||
| main().catch((e) => { | ||
| console.error(e); | ||
| process.exit(1); | ||
| }); | ||
This file was deleted.
Oops, something went wrong.
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.
Since these are educational examples, I would like to preserve these comments.
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.
Hopefully restored
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.
Did you mean restored in this PR or elsewhere? They still seem to be removed in the current diff for this PR.