|
11 | 11 | - [Tools](#tools)
|
12 | 12 | - [Prompts](#prompts)
|
13 | 13 | - [Running Your Server](#running-your-server)
|
14 |
| - - [Development Mode](#development-mode) |
15 |
| - - [Claude Desktop Integration](#claude-desktop-integration) |
16 |
| - - [Direct Execution](#direct-execution) |
| 14 | + - [stdio](#stdio) |
| 15 | + - [HTTP with SSE](#http-with-sse) |
| 16 | + - [Testing and Debugging](#testing-and-debugging) |
17 | 17 | - [Examples](#examples)
|
18 | 18 | - [Echo Server](#echo-server)
|
19 | 19 | - [SQLite Explorer](#sqlite-explorer)
|
@@ -178,6 +178,61 @@ server.prompt(
|
178 | 178 | );
|
179 | 179 | ```
|
180 | 180 |
|
| 181 | +## Running Your Server |
| 182 | + |
| 183 | +MCP servers in TypeScript need to be connected to a transport to communicate with clients. How you start the server depends on the choice of transport: |
| 184 | + |
| 185 | +### stdio |
| 186 | + |
| 187 | +For command-line tools and direct integrations: |
| 188 | + |
| 189 | +```typescript |
| 190 | +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 191 | +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; |
| 192 | + |
| 193 | +const server = new McpServer({ |
| 194 | + name: "example-server", |
| 195 | + version: "1.0.0" |
| 196 | +}); |
| 197 | + |
| 198 | +const transport = new StdioServerTransport(); |
| 199 | +await server.connect(transport); |
| 200 | +``` |
| 201 | + |
| 202 | +### HTTP with SSE |
| 203 | + |
| 204 | +For remote servers, start a web server with a Server-Sent Events (SSE) endpoint, and a separate endpoint for the client to send its messages to: |
| 205 | + |
| 206 | +```typescript |
| 207 | +import express from "express"; |
| 208 | +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 209 | +import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js"; |
| 210 | + |
| 211 | +const app = express(); |
| 212 | +const server = new McpServer({ |
| 213 | + name: "example-server", |
| 214 | + version: "1.0.0" |
| 215 | +}); |
| 216 | + |
| 217 | +app.get("/mcp", async (req, res) => { |
| 218 | + const transport = new SSEServerTransport("/messages", res); |
| 219 | + await server.connect(transport); |
| 220 | +}); |
| 221 | + |
| 222 | +app.post("/messages", async (req, res) => { |
| 223 | + // Note: to support multiple simultaneous connections, these messages will |
| 224 | + // need to be routed to a specific matching transport. (This logic isn't |
| 225 | + // implemented here, for simplicity.) |
| 226 | + await transport.handlePostMessage(req, res); |
| 227 | +}); |
| 228 | + |
| 229 | +app.listen(3000); |
| 230 | +``` |
| 231 | + |
| 232 | +### Testing and Debugging |
| 233 | + |
| 234 | +To test your server, you can use the [MCP Inspector](https://github.com/modelcontextprotocol/inspector). See its README for more information. |
| 235 | + |
181 | 236 | ## Examples
|
182 | 237 |
|
183 | 238 | ### Echo Server
|
|
0 commit comments