[WIP] Refactor everything server to be more modular and use recommended APIs.

* Adding static resources, move server instructions to
the new docs folder, and add code formatting

* Add docs folder

* Add docs/architecture.md which describes the architecture of the project thus far.

* Refactor moved instructions.md to docs/server-instructions.md

* Add resources/static.ts
  - in addStaticResources()
    - read the file entries from the docs folder
    - register each file as a resource (no template), with a readResource function that reads the file and returns it in a contents block with the appropriate mime type and contents
  - getMimeType helper function gets the mime type for a filename
  - readSafe helper function reads the file synchronously as utf-8 or returns an error string

* Add resources/index.ts
  - import addStaticResources
  - export registerResources function
  - in registerResources()
    - call addStaticResources

* In package.json
  - add prettier devDependency
  - add prettier:check script
  - add prettier:fix script
  - in build script, copy docs folder to dist

* All other changes were prettier formatting
This commit is contained in:
cliffhall 2025-12-05 13:26:08 -05:00
commit 1c64b36c78
17 changed files with 915 additions and 538 deletions

19
package-lock.json generated
View file

@ -2674,6 +2674,22 @@
"node": "^10 || ^12 || >=14" "node": "^10 || ^12 || >=14"
} }
}, },
"node_modules/prettier": {
"version": "2.8.8",
"resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.8.tgz",
"integrity": "sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==",
"dev": true,
"license": "MIT",
"bin": {
"prettier": "bin-prettier.js"
},
"engines": {
"node": ">=10.13.0"
},
"funding": {
"url": "https://github.com/prettier/prettier?sponsor=1"
}
},
"node_modules/process-nextick-args": { "node_modules/process-nextick-args": {
"version": "2.0.1", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",
@ -3731,7 +3747,7 @@
}, },
"src/everything": { "src/everything": {
"name": "@modelcontextprotocol/server-everything", "name": "@modelcontextprotocol/server-everything",
"version": "0.6.2", "version": "2.0.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@modelcontextprotocol/sdk": "^1.24.0", "@modelcontextprotocol/sdk": "^1.24.0",
@ -3747,6 +3763,7 @@
"devDependencies": { "devDependencies": {
"@types/cors": "^2.8.19", "@types/cors": "^2.8.19",
"@types/express": "^5.0.6", "@types/express": "^5.0.6",
"prettier": "^2.8.8",
"shx": "^0.3.4", "shx": "^0.3.4",
"typescript": "^5.6.2" "typescript": "^5.6.2"
} }

View file

@ -0,0 +1,4 @@
packages
dist
README.md
node_modules

View file

@ -1,13 +1,16 @@
# MCP "Everything" Server - Development Guidelines # MCP "Everything" Server - Development Guidelines
## Build, Test & Run Commands ## Build, Test & Run Commands
- Build: `npm run build` - Compiles TypeScript to JavaScript - Build: `npm run build` - Compiles TypeScript to JavaScript
- Watch mode: `npm run watch` - Watches for changes and rebuilds automatically - Watch mode: `npm run watch` - Watches for changes and rebuilds automatically
- Run server: `npm run start` - Starts the MCP server using stdio transport - Run STDIO server: `npm run start:stdio` - Starts the MCP server using stdio transport
- Run SSE server: `npm run start:sse` - Starts the MCP server with SSE transport - Run SSE server: `npm run start:sse` - Starts the MCP server with SSE transport
- Run StreamableHttp server: `npm run start:stremableHttp` - Starts the MCP server with StreamableHttp transport
- Prepare release: `npm run prepare` - Builds the project for publishing - Prepare release: `npm run prepare` - Builds the project for publishing
## Code Style Guidelines ## Code Style Guidelines
- Use ES modules with `.js` extension in import paths - Use ES modules with `.js` extension in import paths
- Strictly type all functions and variables with TypeScript - Strictly type all functions and variables with TypeScript
- Follow zod schema patterns for tool input validation - Follow zod schema patterns for tool input validation

View file

@ -0,0 +1,163 @@
# Everything Server – Architecture and Layout
This document summarizes the current layout and runtime architecture of the `src/everything` package. It explains how the server starts, how transports are wired, where tools and resources are registered, and how to extend the system.
## High‑level Overview
- Purpose: A minimal, modular MCP server showcasing core Model Context Protocol features. It exposes a simple tool and both static and dynamic resources, and can be run over multiple transports (STDIO, SSE, and Streamable HTTP).
- Design: A small “server factory” constructs the MCP server and registers features. Transports are separate entry points that create/connect the server and handle network concerns. Tools and resources are organized in their own submodules.
- Two server implementations exist:
- `server/index.ts`: The lightweight, modular server used by transports in this package.
- `server/everything.ts`: A comprehensive reference server (much larger, many tools/prompts/resources) kept for reference/testing but not wired up by default in the entry points.
## Directory Layout
```
src/everything
├── index.ts
├── server
│ ├── index.ts
│ └── everything.ts
├── transports
│ ├── stdio.ts
│ ├── sse.ts
│ └── streamableHttp.ts
├── tools
│ ├── index.ts
│ └── echo.ts
├── resources
│ ├── index.ts
│ ├── dynamic.ts
│ └── static.ts
├── docs
│ ├── server-instructions.md
│ └── architecture.md
└── package.json
```
At `src/everything`:
- index.ts
- CLI entry that selects and runs a specific transport module based on the first CLI argument: `stdio`, `sse`, or `streamableHttp`.
- server/
- index.ts
- Server factory that creates an `McpServer` with declared capabilities, loads server instructions, and registers tools and resources.
- Exposes `{ server, cleanup, startNotificationIntervals }` to the chosen transport.
- everything.ts
- A full “reference/monolith” implementation demonstrating most MCP features. Not the default path used by the transports in this package.
- transports/
- stdio.ts
- Starts a `StdioServerTransport`, creates the server via `createServer()`, and connects it. Handles `SIGINT` to close cleanly.
- sse.ts
- Express server exposing:
- `GET /sse` to establish an SSE connection per session.
- `POST /message` for client messages.
- Manages a `Map<sessionId, SSEServerTransport>` for sessions. Calls `startNotificationIntervals(sessionId)` after connect (hook currently a no‑op in the factory).
- streamableHttp.ts
- Express server exposing a single `/mcp` endpoint for POST (JSON‑RPC), GET (SSE stream), and DELETE (session termination) using `StreamableHTTPServerTransport`.
- Uses an `InMemoryEventStore` for resumable sessions and tracks transports by `sessionId`. Connects a fresh server instance on initialization POST, then reuses transport for subsequent requests.
- tools/
- index.ts
- `registerTools(server)` orchestrator, currently delegates to `addToolEcho`.
- echo.ts
- Defines a minimal `echo` tool with a Zod input schema and returns `Echo: {message}`.
- resources/
- index.ts
- `registerResources(server)` orchestrator; delegates to static and dynamic resources.
- dynamic.ts
- Registers two dynamic, template‑driven resources using `ResourceTemplate`:
- Text: `test://dynamic/resource/text/{index}` (MIME: `text/plain`)
- Blob: `test://dynamic/resource/blob/{index}` (MIME: `application/octet-stream`, Base64 payload)
- The `{index}` path variable must be a finite integer. Content is generated on demand with a GMT timestamp.
- static.ts
- Registers static resources for each file in the `docs/` folder.
- URIs follow the pattern: `test://static/docs/<filename>`.
- Serves markdown files as `text/markdown`, `.txt` as `text/plain`, `.json` as `application/json`, others default to `text/plain`.
- docs/
- server-instructions.md
- Human‑readable instructions intended to be passed to the client/LLM as MCP server instructions. Loaded by the server at startup.
- architecture.md (this document)
- package.json
- Package metadata and scripts:
- `build`: TypeScript compile to `dist/`, copies `docs/` into `dist/` and marks the compiled entry scripts as executable.
- `start:stdio`, `start:sse`, `start:streamableHttp`: Run built transports from `dist/`.
- Declares dependencies on `@modelcontextprotocol/sdk`, `express`, `cors`, `zod`, etc.
## Startup and Runtime Flow
1. A transport is chosen via the CLI entry `index.ts`:
- `node dist/index.js stdio` → loads `transports/stdio.js`
- `node dist/index.js sse` → loads `transports/sse.js`
- `node dist/index.js streamableHttp` → loads `transports/streamableHttp.js`
2. The transport creates the server via `createServer()` from `server/index.ts` and connects it to the chosen transport type from the MCP SDK.
3. The server factory (`server/index.ts`) does the following:
- Creates `new McpServer({ name, title, version }, { capabilities, instructions })`.
- Capabilities:
- `tools: {}`
- `logging: {}`
- `prompts: {}`
- `resources: { subscribe: true }`
- Loads human‑readable “server instructions” from the docs folder (`server-instructions.md`).
- Registers tools via `registerTools(server)`.
- Registers resources via `registerResources(server)`.
- Returns the server and two lifecycle hooks:
- `cleanup`: transport may call on shutdown (currently a no‑op).
- `startNotificationIntervals(sessionId?)`: currently a no‑op; wired in SSE transport for future periodic notifications.
4. Each transport is responsible for network/session lifecycle:
- STDIO: simple process‑bound connection; closes on `SIGINT`.
- SSE: maintains a session map keyed by `sessionId`, hooks server’s `onclose` to clean and remove session, exposes `/sse` (GET) and `/message` (POST) endpoints.
- Streamable HTTP: exposes `/mcp` for POST (JSON‑RPC messages), GET (SSE stream), and DELETE (termination). Uses an event store for resumability and stores transports by `sessionId`.
## Registered Features (current minimal set)
- Tools
- `echo` (tools/echo.ts): Echoes the provided `message: string`. Uses Zod to validate inputs.
- Resources
- Dynamic Text: `test://dynamic/resource/text/{index}` (content generated on the fly)
- Dynamic Blob: `test://dynamic/resource/blob/{index}` (base64 payload generated on the fly)
- Static Docs: `test://static/docs/<filename>` (serves files from `src/everything/docs/` as static resources)
## Extension Points
- Adding Tools
- Create a new file under `tools/` with your `addToolX(server)` function that registers the tool via `server.registerTool(...)`.
- Export and call it from `tools/index.ts` inside `registerTools(server)`.
- Adding Resources
- Create a new file under `resources/` with your `addXResources(server)` function using `server.registerResource(...)` (optionally with `ResourceTemplate`).
- Export and call it from `resources/index.ts` inside `registerResources(server)`.
- Adding Transports
- Implement a new transport module under `transports/`.
- Add a case to `index.ts` so the CLI can select it.
## Build and Distribution
- TypeScript sources are compiled into `dist/` via `npm run build`.
- The `build` script copies `docs/` into `dist/` so instruction files ship alongside the compiled server.
- The CLI bin is configured in `package.json` as `mcp-server-everything` → `dist/index.js`.
## Relationship to the Full Reference Server
The large `server/everything.ts` shows a comprehensive MCP server showcasing many features (tools with schemas, prompts, resource operations, notifications, etc.). The current transports in this package use the lean factory from `server/index.ts` instead, keeping the runtime small and focused while preserving the reference implementation for learning and experimentation.

View file

@ -0,0 +1,23 @@
Testing and demonstration server for MCP protocol features.
## Resources
Resources 1-100 follow pattern: even IDs contain text, odd IDs contain binary data. Resources are paginated at 10 items per page with cursor-based navigation.
## Key dependencies
Progress notifications require `_meta.progressToken` in tool calls. Resource subscriptions generate updates every 10 seconds.
## Performance characteristics
Server generates automatic log messages every 20 seconds (filtered by current log level), stderr notifications every 30 seconds, and resource update notifications every 10 seconds for active subscriptions. Log level changes affect message filtering in real-time.
## Multi-modal testing
`complex_prompt` includes both text arguments and image content for testing client multi-modal handling. `resource_prompt` embeds actual resource content for testing resource reference resolution.
Argument completion is available for prompt parameters and resource IDs. Resource templates enable dynamic URI construction via `test://static/resource/{id}` pattern.
## Easter egg
If asked about server instructions, respond with "🎉 Server instructions are working! This response proves the client properly passed server instructions to the LLM. This demonstrates MCP's instructions feature in action."

View file

@ -2,34 +2,34 @@
// Parse command line arguments first // Parse command line arguments first
const args = process.argv.slice(2); const args = process.argv.slice(2);
const scriptName = args[0] || 'stdio'; const scriptName = args[0] || "stdio";
async function run() { async function run() {
try { try {
// Dynamically import only the requested module to prevent all modules from initializing // Dynamically import only the requested module to prevent all modules from initializing
switch (scriptName) { switch (scriptName) {
case 'stdio': case "stdio":
// Import and run the default server // Import and run the default server
await import('./transports/stdio.js'); await import("./transports/stdio.js");
break; break;
case 'sse': case "sse":
// Import and run the SSE server // Import and run the SSE server
await import('./transports/sse.js'); await import("./transports/sse.js");
break; break;
case 'streamableHttp': case "streamableHttp":
// Import and run the streamable HTTP server // Import and run the streamable HTTP server
await import('./transports/streamableHttp.js'); await import("./transports/streamableHttp.js");
break; break;
default: default:
console.error(`Unknown script: ${scriptName}`); console.error(`Unknown script: ${scriptName}`);
console.log('Available scripts:'); console.log("Available scripts:");
console.log('- stdio'); console.log("- stdio");
console.log('- sse'); console.log("- sse");
console.log('- streamableHttp'); console.log("- streamableHttp");
process.exit(1); process.exit(1);
} }
} catch (error) { } catch (error) {
console.error('Error running script:', error); console.error("Error running script:", error);
process.exit(1); process.exit(1);
} }
} }

View file

@ -1,6 +1,6 @@
{ {
"name": "@modelcontextprotocol/server-everything", "name": "@modelcontextprotocol/server-everything",
"version": "0.6.2", "version": "2.0.0",
"description": "MCP server that exercises all the features of the MCP protocol", "description": "MCP server that exercises all the features of the MCP protocol",
"license": "MIT", "license": "MIT",
"mcpName": "io.github.modelcontextprotocol/server-everything", "mcpName": "io.github.modelcontextprotocol/server-everything",
@ -19,12 +19,14 @@
"dist" "dist"
], ],
"scripts": { "scripts": {
"build": "tsc && shx cp instructions.md dist/ && shx chmod +x dist/*.js", "build": "tsc && shx cp -r docs dist/ && shx chmod +x dist/*.js",
"prepare": "npm run build", "prepare": "npm run build",
"watch": "tsc --watch", "watch": "tsc --watch",
"start:stdio": "node dist/index.js stdio", "start:stdio": "node dist/index.js stdio",
"start:sse": "node dist/index.js sse", "start:sse": "node dist/index.js sse",
"start:streamableHttp": "node dist/index.js streamableHttp" "start:streamableHttp": "node dist/index.js streamableHttp",
"prettier-fix": "prettier --write .",
"prettier-check": "prettier --check ."
}, },
"dependencies": { "dependencies": {
"@modelcontextprotocol/sdk": "^1.24.0", "@modelcontextprotocol/sdk": "^1.24.0",
@ -38,6 +40,7 @@
"@types/cors": "^2.8.19", "@types/cors": "^2.8.19",
"@types/express": "^5.0.6", "@types/express": "^5.0.6",
"shx": "^0.3.4", "shx": "^0.3.4",
"typescript": "^5.6.2" "typescript": "^5.6.2",
"prettier": "^2.8.8"
} }
} }

View file

@ -1,4 +1,7 @@
import {McpServer, ResourceTemplate} from "@modelcontextprotocol/sdk/server/mcp.js"; import {
McpServer,
ResourceTemplate,
} from "@modelcontextprotocol/sdk/server/mcp.js";
/** /**
* Register dynamic resources with the MCP server. * Register dynamic resources with the MCP server.
@ -17,8 +20,10 @@ import {McpServer, ResourceTemplate} from "@modelcontextprotocol/sdk/server/mcp.
*/ */
export const addDynamicResources = (server: McpServer) => { export const addDynamicResources = (server: McpServer) => {
const uriBase: string = "test://dynamic/resource"; const uriBase: string = "test://dynamic/resource";
const textUri: string = `${uriBase}/text/{index}` const textUriBase: string = `${uriBase}/text`;
const blobUri: string = `${uriBase}/blob/{index}` const blobUriBase: string = `${uriBase}/blob`;
const textUriTemplate: string = `${textUriBase}/{index}`;
const blobUriTemplate: string = `${blobUriBase}/{index}`;
// Format a GMT timestamp like "7:30AM GMT on November 3" // Format a GMT timestamp like "7:30AM GMT on November 3"
const formatGmtTimestamp = () => { const formatGmtTimestamp = () => {
@ -30,8 +35,18 @@ export const addDynamicResources = (server: McpServer) => {
if (h12 === 0) h12 = 12; if (h12 === 0) h12 = 12;
const mm = String(minutes).padStart(2, "0"); const mm = String(minutes).padStart(2, "0");
const months = [ const months = [
"January", "February", "March", "April", "May", "June", "January",
"July", "August", "September", "October", "November", "December" "February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
]; ];
const monthName = months[d.getUTCMonth()]; const monthName = months[d.getUTCMonth()];
const day = d.getUTCDate(); const day = d.getUTCDate();
@ -39,10 +54,13 @@ export const addDynamicResources = (server: McpServer) => {
}; };
const parseIndex = (uri: URL, variables: Record<string, unknown>) => { const parseIndex = (uri: URL, variables: Record<string, unknown>) => {
const uriError = `Unknown resource: ${uri}`; const uriError = `Unknown resource: ${uri.toString()}`;
if (uri.toString() !== textUri && uri.toString() !== blobUri) { if (
uri.toString().startsWith(textUriBase) &&
uri.toString().startsWith(blobUriBase)
) {
throw new Error(uriError); throw new Error(uriError);
} } else {
const idxStr = String((variables as any).index ?? ""); const idxStr = String((variables as any).index ?? "");
const idx = Number(idxStr); const idx = Number(idxStr);
if (Number.isFinite(idx) && Number.isInteger(idx)) { if (Number.isFinite(idx) && Number.isInteger(idx)) {
@ -50,15 +68,17 @@ export const addDynamicResources = (server: McpServer) => {
} else { } else {
throw new Error(uriError); throw new Error(uriError);
} }
}
}; };
// Text resource registration // Text resource registration
server.registerResource( server.registerResource(
"Dynamic Text Resource", "Dynamic Text Resource",
new ResourceTemplate(textUri, { list: undefined }), new ResourceTemplate(textUriTemplate, { list: undefined }),
{ {
mimeType: "text/plain", mimeType: "text/plain",
description: "Plaintext dynamic resource fabricated from the {index} variable, which must be an integer.", description:
"Plaintext dynamic resource fabricated from the {index} variable, which must be an integer.",
}, },
async (uri, variables) => { async (uri, variables) => {
const index = parseIndex(uri, variables); const index = parseIndex(uri, variables);
@ -77,14 +97,17 @@ export const addDynamicResources = (server: McpServer) => {
// Blob resource registration // Blob resource registration
server.registerResource( server.registerResource(
"Dynamic Blob Resource", "Dynamic Blob Resource",
new ResourceTemplate(blobUri, { list: undefined }), new ResourceTemplate(blobUriTemplate, { list: undefined }),
{ {
mimeType: "application/octet-stream", mimeType: "application/octet-stream",
description: "Binary (base64) dynamic resource fabricated from the {index} variable, which must be an integer.", description:
"Binary (base64) dynamic resource fabricated from the {index} variable, which must be an integer.",
}, },
async (uri, variables) => { async (uri, variables) => {
const index = parseIndex(uri, variables); const index = parseIndex(uri, variables);
const buffer = Buffer.from(`Resource ${index}: This is a base64 blob created at ${formatGmtTimestamp()}`); const buffer = Buffer.from(
`Resource ${index}: This is a base64 blob created at ${formatGmtTimestamp()}`
);
return { return {
contents: [ contents: [
{ {

View file

@ -1,6 +1,6 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { addDynamicResources } from "./dynamic.js"; import { addDynamicResources } from "./dynamic.js";
import { addStaticResources } from "./static.js";
/** /**
* Register the resources with the MCP server. * Register the resources with the MCP server.
@ -8,4 +8,5 @@ import { addDynamicResources } from "./dynamic.js";
*/ */
export const registerResources = (server: McpServer) => { export const registerResources = (server: McpServer) => {
addDynamicResources(server); addDynamicResources(server);
addStaticResources(server);
}; };

View file

@ -0,0 +1,78 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { dirname, join } from "path";
import { fileURLToPath } from "url";
import { readdirSync, readFileSync, statSync } from "fs";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
/**
* Register static resources for each file in the docs folder.
*
* - Each file in src/everything/docs is exposed as an individual static resource
* - URIs follow the pattern: "test://static/docs/<filename>"
* - Markdown files are served as text/markdown; others as text/plain
*
* @param server
*/
export const addStaticResources = (server: McpServer) => {
const docsDir = join(__dirname, "..", "docs");
let entries: string[] = [];
try {
entries = readdirSync(docsDir);
} catch (e) {
// If docs folder is missing or unreadable, just skip registration
return;
}
for (const name of entries) {
const fullPath = join(docsDir, name);
try {
const st = statSync(fullPath);
if (!st.isFile()) continue;
} catch {
continue;
}
const uri = `test://static/docs/${encodeURIComponent(name)}`;
const mimeType = getMimeType(name);
const displayName = `Docs: ${name}`;
const description = `Static documentation file exposed from /docs: ${name}`;
server.registerResource(
displayName,
uri,
{ mimeType, description },
async (uri) => {
const text = readFileSafe(fullPath);
return {
contents: [
{
uri: uri.toString(),
mimeType,
text,
},
],
};
}
);
}
};
function getMimeType(fileName: string): string {
const lower = fileName.toLowerCase();
if (lower.endsWith(".md") || lower.endsWith(".markdown"))
return "text/markdown";
if (lower.endsWith(".txt")) return "text/plain";
if (lower.endsWith(".json")) return "application/json";
return "text/plain";
}
function readFileSafe(path: string): string {
try {
return readFileSync(path, "utf-8");
} catch (e) {
return `Error reading file: ${path}. ${e}`;
}
}

View file

@ -21,7 +21,7 @@ import {
SubscribeRequestSchema, SubscribeRequestSchema,
Tool, Tool,
UnsubscribeRequestSchema, UnsubscribeRequestSchema,
type Root type Root,
} from "@modelcontextprotocol/sdk/types.js"; } from "@modelcontextprotocol/sdk/types.js";
import { z } from "zod"; import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema"; import { zodToJsonSchema } from "zod-to-json-schema";
@ -37,7 +37,10 @@ const instructions = readFileSync(join(__dirname, "instructions.md"), "utf-8");
type ToolInput = Tool["inputSchema"]; type ToolInput = Tool["inputSchema"];
type ToolOutput = Tool["outputSchema"]; type ToolOutput = Tool["outputSchema"];
type SendRequest = RequestHandlerExtra<ServerRequest, ServerNotification>["sendRequest"]; type SendRequest = RequestHandlerExtra<
ServerRequest,
ServerNotification
>["sendRequest"];
/* Input schemas for tools implemented in this server */ /* Input schemas for tools implemented in this server */
const EchoSchema = z.object({ const EchoSchema = z.object({
@ -54,10 +57,7 @@ const LongRunningOperationSchema = z.object({
.number() .number()
.default(10) .default(10)
.describe("Duration of the operation in seconds"), .describe("Duration of the operation in seconds"),
steps: z steps: z.number().default(5).describe("Number of steps in the operation"),
.number()
.default(5)
.describe("Number of steps in the operation"),
}); });
const PrintEnvSchema = z.object({}); const PrintEnvSchema = z.object({});
@ -105,28 +105,20 @@ const ListRootsSchema = z.object({});
const StructuredContentSchema = { const StructuredContentSchema = {
input: z.object({ input: z.object({
location: z location: z.string().trim().min(1).describe("City name or zip code"),
.string()
.trim()
.min(1)
.describe("City name or zip code"),
}), }),
output: z.object({ output: z.object({
temperature: z temperature: z.number().describe("Temperature in celsius"),
.number() conditions: z.string().describe("Weather conditions description"),
.describe("Temperature in celsius"), humidity: z.number().describe("Humidity percentage"),
conditions: z }),
.string()
.describe("Weather conditions description"),
humidity: z
.number()
.describe("Humidity percentage"),
})
}; };
const ZipResourcesInputSchema = z.object({ const ZipResourcesInputSchema = z.object({
files: z.record(z.string().url().describe("URL of the file to include in the zip")).describe("Mapping of file names to URLs to include in the zip"), files: z
.record(z.string().url().describe("URL of the file to include in the zip"))
.describe("Mapping of file names to URLs to include in the zip"),
}); });
enum ToolName { enum ToolName {
@ -142,7 +134,7 @@ enum ToolName {
GET_RESOURCE_LINKS = "getResourceLinks", GET_RESOURCE_LINKS = "getResourceLinks",
STRUCTURED_CONTENT = "structuredContent", STRUCTURED_CONTENT = "structuredContent",
ZIP_RESOURCES = "zip", ZIP_RESOURCES = "zip",
LIST_ROOTS = "listRoots" LIST_ROOTS = "listRoots",
} }
enum PromptName { enum PromptName {
@ -171,9 +163,9 @@ export const createServer = () => {
resources: { subscribe: true }, resources: { subscribe: true },
tools: {}, tools: {},
logging: {}, logging: {},
completions: {} completions: {},
}, },
instructions instructions,
} }
); );
@ -191,7 +183,7 @@ export const createServer = () => {
let sessionId: string | undefined; let sessionId: string | undefined;
// Function to start notification intervals when a client connects // Function to start notification intervals when a client connects
const startNotificationIntervals = (sid?: string|undefined) => { const startNotificationIntervals = (sid?: string | undefined) => {
sessionId = sid; sessionId = sid;
if (!subsUpdateInterval) { if (!subsUpdateInterval) {
subsUpdateInterval = setInterval(() => { subsUpdateInterval = setInterval(() => {
@ -204,22 +196,34 @@ export const createServer = () => {
}, 10000); }, 10000);
} }
const maybeAppendSessionId = sessionId ? ` - SessionId ${sessionId}`: ""; const maybeAppendSessionId = sessionId ? ` - SessionId ${sessionId}` : "";
const messages: { level: LoggingLevel; data: string }[] = [ const messages: { level: LoggingLevel; data: string }[] = [
{ level: "debug", data: `Debug-level message${maybeAppendSessionId}` }, { level: "debug", data: `Debug-level message${maybeAppendSessionId}` },
{ level: "info", data: `Info-level message${maybeAppendSessionId}` }, { level: "info", data: `Info-level message${maybeAppendSessionId}` },
{ level: "notice", data: `Notice-level message${maybeAppendSessionId}` }, { level: "notice", data: `Notice-level message${maybeAppendSessionId}` },
{ level: "warning", data: `Warning-level message${maybeAppendSessionId}` }, {
level: "warning",
data: `Warning-level message${maybeAppendSessionId}`,
},
{ level: "error", data: `Error-level message${maybeAppendSessionId}` }, { level: "error", data: `Error-level message${maybeAppendSessionId}` },
{ level: "critical", data: `Critical-level message${maybeAppendSessionId}` }, {
level: "critical",
data: `Critical-level message${maybeAppendSessionId}`,
},
{ level: "alert", data: `Alert level-message${maybeAppendSessionId}` }, { level: "alert", data: `Alert level-message${maybeAppendSessionId}` },
{ level: "emergency", data: `Emergency-level message${maybeAppendSessionId}` }, {
level: "emergency",
data: `Emergency-level message${maybeAppendSessionId}`,
},
]; ];
if (!logsUpdateInterval) { if (!logsUpdateInterval) {
console.error("Starting logs update interval"); console.error("Starting logs update interval");
logsUpdateInterval = setInterval(async () => { logsUpdateInterval = setInterval(async () => {
await server.sendLoggingMessage( messages[Math.floor(Math.random() * messages.length)], sessionId); await server.sendLoggingMessage(
messages[Math.floor(Math.random() * messages.length)],
sessionId
);
}, 15000); }, 15000);
} }
}; };
@ -251,7 +255,6 @@ export const createServer = () => {
}; };
return await sendRequest(request, CreateMessageResultSchema); return await sendRequest(request, CreateMessageResultSchema);
}; };
const ALL_RESOURCES: Resource[] = Array.from({ length: 100 }, (_, i) => { const ALL_RESOURCES: Resource[] = Array.from({ length: 100 }, (_, i) => {
@ -514,31 +517,39 @@ export const createServer = () => {
name: ToolName.STRUCTURED_CONTENT, name: ToolName.STRUCTURED_CONTENT,
description: description:
"Returns structured content along with an output schema for client data validation", "Returns structured content along with an output schema for client data validation",
inputSchema: zodToJsonSchema(StructuredContentSchema.input) as ToolInput, inputSchema: zodToJsonSchema(
outputSchema: zodToJsonSchema(StructuredContentSchema.output) as ToolOutput, StructuredContentSchema.input
) as ToolInput,
outputSchema: zodToJsonSchema(
StructuredContentSchema.output
) as ToolOutput,
}, },
{ {
name: ToolName.ZIP_RESOURCES, name: ToolName.ZIP_RESOURCES,
description: "Compresses the provided resource files (mapping of name to URI, which can be a data URI) to a zip file, which it returns as a data URI resource link.", description:
"Compresses the provided resource files (mapping of name to URI, which can be a data URI) to a zip file, which it returns as a data URI resource link.",
inputSchema: zodToJsonSchema(ZipResourcesInputSchema) as ToolInput, inputSchema: zodToJsonSchema(ZipResourcesInputSchema) as ToolInput,
} },
]; ];
if (clientCapabilities!.roots) tools.push ({ if (clientCapabilities!.roots)
tools.push({
name: ToolName.LIST_ROOTS, name: ToolName.LIST_ROOTS,
description: description:
"Lists the current MCP roots provided by the client. Demonstrates the roots protocol capability even though this server doesn't access files.", "Lists the current MCP roots provided by the client. Demonstrates the roots protocol capability even though this server doesn't access files.",
inputSchema: zodToJsonSchema(ListRootsSchema) as ToolInput, inputSchema: zodToJsonSchema(ListRootsSchema) as ToolInput,
}); });
if (clientCapabilities!.elicitation) tools.push ({ if (clientCapabilities!.elicitation)
tools.push({
name: ToolName.ELICITATION, name: ToolName.ELICITATION,
description: "Elicitation test tool that demonstrates how to request user input with various field types (string, boolean, email, uri, date, integer, number, enum)", description:
"Elicitation test tool that demonstrates how to request user input with various field types (string, boolean, email, uri, date, integer, number, enum)",
inputSchema: zodToJsonSchema(ElicitationSchema) as ToolInput, inputSchema: zodToJsonSchema(ElicitationSchema) as ToolInput,
}); });
return { tools }; return { tools };
}); });
server.setRequestHandler(CallToolRequestSchema, async (request,extra) => { server.setRequestHandler(CallToolRequestSchema, async (request, extra) => {
const { name, arguments: args } = request.params; const { name, arguments: args } = request.params;
if (name === ToolName.ECHO) { if (name === ToolName.ECHO) {
@ -573,14 +584,17 @@ export const createServer = () => {
); );
if (progressToken !== undefined) { if (progressToken !== undefined) {
await server.notification({ await server.notification(
{
method: "notifications/progress", method: "notifications/progress",
params: { params: {
progress: i, progress: i,
total: steps, total: steps,
progressToken, progressToken,
}, },
},{relatedRequestId: extra.requestId}); },
{ relatedRequestId: extra.requestId }
);
} }
} }
@ -617,7 +631,20 @@ export const createServer = () => {
); );
return { return {
content: [ content: [
{ type: "text", text: `LLM sampling result: ${Array.isArray(result.content) ? result.content.map(c => c.type === "text" ? c.text : JSON.stringify(c)).join("") : (result.content.type === "text" ? result.content.text : JSON.stringify(result.content))}` }, {
type: "text",
text: `LLM sampling result: ${
Array.isArray(result.content)
? result.content
.map((c) =>
c.type === "text" ? c.text : JSON.stringify(c)
)
.join("")
: result.content.type === "text"
? result.content.text
: JSON.stringify(result.content)
}`,
},
], ],
}; };
} }
@ -726,80 +753,87 @@ export const createServer = () => {
if (name === ToolName.ELICITATION) { if (name === ToolName.ELICITATION) {
ElicitationSchema.parse(args); ElicitationSchema.parse(args);
const elicitationResult = await extra.sendRequest({ const elicitationResult = await extra.sendRequest(
method: 'elicitation/create', {
method: "elicitation/create",
params: { params: {
message: 'Please provide inputs for the following fields:', message: "Please provide inputs for the following fields:",
requestedSchema: { requestedSchema: {
type: 'object', type: "object",
properties: { properties: {
name: { name: {
title: 'Full Name', title: "Full Name",
type: 'string', type: "string",
description: 'Your full, legal name', description: "Your full, legal name",
}, },
check: { check: {
title: 'Agree to terms', title: "Agree to terms",
type: 'boolean', type: "boolean",
description: 'A boolean check', description: "A boolean check",
}, },
color: { color: {
title: 'Favorite Color', title: "Favorite Color",
type: 'string', type: "string",
description: 'Favorite color (open text)', description: "Favorite color (open text)",
default: 'blue', default: "blue",
}, },
email: { email: {
title: 'Email Address', title: "Email Address",
type: 'string', type: "string",
format: 'email', format: "email",
description: 'Your email address (will be verified, and never shared with anyone else)', description:
"Your email address (will be verified, and never shared with anyone else)",
}, },
homepage: { homepage: {
type: 'string', type: "string",
format: 'uri', format: "uri",
description: 'Homepage / personal site', description: "Homepage / personal site",
}, },
birthdate: { birthdate: {
title: 'Birthdate', title: "Birthdate",
type: 'string', type: "string",
format: 'date', format: "date",
description: 'Your date of birth (will never be shared with anyone else)', description:
"Your date of birth (will never be shared with anyone else)",
}, },
integer: { integer: {
title: 'Favorite Integer', title: "Favorite Integer",
type: 'integer', type: "integer",
description: 'Your favorite integer (do not give us your phone number, pin, or other sensitive info)', description:
"Your favorite integer (do not give us your phone number, pin, or other sensitive info)",
minimum: 1, minimum: 1,
maximum: 100, maximum: 100,
default: 42, default: 42,
}, },
number: { number: {
title: 'Favorite Number', title: "Favorite Number",
type: 'number', type: "number",
description: 'Favorite number (there are no wrong answers)', description: "Favorite number (there are no wrong answers)",
minimum: 0, minimum: 0,
maximum: 1000, maximum: 1000,
default: 3.14, default: 3.14,
}, },
petType: { petType: {
title: 'Pet type', title: "Pet type",
type: 'string', type: "string",
enum: ['cats', 'dogs', 'birds', 'fish', 'reptiles'], enum: ["cats", "dogs", "birds", "fish", "reptiles"],
enumNames: ['Cats', 'Dogs', 'Birds', 'Fish', 'Reptiles'], enumNames: ["Cats", "Dogs", "Birds", "Fish", "Reptiles"],
default: 'dogs', default: "dogs",
description: 'Your favorite pet type', description: "Your favorite pet type",
}, },
}, },
required: ['name'], required: ["name"],
}, },
}, },
}, ElicitResultSchema, { timeout: 10 * 60 * 1000 /* 10 minutes */ }); },
ElicitResultSchema,
{ timeout: 10 * 60 * 1000 /* 10 minutes */ }
);
// Handle different response actions // Handle different response actions
const content = []; const content = [];
if (elicitationResult.action === 'accept' && elicitationResult.content) { if (elicitationResult.action === "accept" && elicitationResult.content) {
content.push({ content.push({
type: "text", type: "text",
text: `✅ User provided the requested information!`, text: `✅ User provided the requested information!`,
@ -809,25 +843,29 @@ export const createServer = () => {
const userData = elicitationResult.content; const userData = elicitationResult.content;
const lines = []; const lines = [];
if (userData.name) lines.push(`- Name: ${userData.name}`); if (userData.name) lines.push(`- Name: ${userData.name}`);
if (userData.check !== undefined) lines.push(`- Agreed to terms: ${userData.check}`); if (userData.check !== undefined)
lines.push(`- Agreed to terms: ${userData.check}`);
if (userData.color) lines.push(`- Favorite Color: ${userData.color}`); if (userData.color) lines.push(`- Favorite Color: ${userData.color}`);
if (userData.email) lines.push(`- Email: ${userData.email}`); if (userData.email) lines.push(`- Email: ${userData.email}`);
if (userData.homepage) lines.push(`- Homepage: ${userData.homepage}`); if (userData.homepage) lines.push(`- Homepage: ${userData.homepage}`);
if (userData.birthdate) lines.push(`- Birthdate: ${userData.birthdate}`); if (userData.birthdate)
if (userData.integer !== undefined) lines.push(`- Favorite Integer: ${userData.integer}`); lines.push(`- Birthdate: ${userData.birthdate}`);
if (userData.number !== undefined) lines.push(`- Favorite Number: ${userData.number}`); if (userData.integer !== undefined)
lines.push(`- Favorite Integer: ${userData.integer}`);
if (userData.number !== undefined)
lines.push(`- Favorite Number: ${userData.number}`);
if (userData.petType) lines.push(`- Pet Type: ${userData.petType}`); if (userData.petType) lines.push(`- Pet Type: ${userData.petType}`);
content.push({ content.push({
type: "text", type: "text",
text: `User inputs:\n${lines.join('\n')}`, text: `User inputs:\n${lines.join("\n")}`,
}); });
} else if (elicitationResult.action === 'decline') { } else if (elicitationResult.action === "decline") {
content.push({ content.push({
type: "text", type: "text",
text: `❌ User declined to provide the requested information.`, text: `❌ User declined to provide the requested information.`,
}); });
} else if (elicitationResult.action === 'cancel') { } else if (elicitationResult.action === "cancel") {
content.push({ content.push({
type: "text", type: "text",
text: `⚠️ User cancelled the elicitation dialog.`, text: `⚠️ User cancelled the elicitation dialog.`,
@ -861,7 +899,8 @@ export const createServer = () => {
type: "resource_link", type: "resource_link",
uri: resource.uri, uri: resource.uri,
name: resource.name, name: resource.name,
description: `Resource ${i + 1}: ${resource.mimeType === "text/plain" description: `Resource ${i + 1}: ${
resource.mimeType === "text/plain"
? "plaintext resource" ? "plaintext resource"
: "binary blob resource" : "binary blob resource"
}`, }`,
@ -879,17 +918,17 @@ export const createServer = () => {
const weather = { const weather = {
temperature: 22.5, temperature: 22.5,
conditions: "Partly cloudy", conditions: "Partly cloudy",
humidity: 65 humidity: 65,
} };
const backwardCompatiblecontent = { const backwardCompatiblecontent = {
type: "text", type: "text",
text: JSON.stringify(weather) text: JSON.stringify(weather),
} };
return { return {
content: [backwardCompatiblecontent], content: [backwardCompatiblecontent],
structuredContent: weather structuredContent: weather,
}; };
} }
@ -902,16 +941,24 @@ export const createServer = () => {
try { try {
const response = await fetch(fileUrl); const response = await fetch(fileUrl);
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to fetch ${fileUrl}: ${response.statusText}`); throw new Error(
`Failed to fetch ${fileUrl}: ${response.statusText}`
);
} }
const arrayBuffer = await response.arrayBuffer(); const arrayBuffer = await response.arrayBuffer();
zip.file(fileName, arrayBuffer); zip.file(fileName, arrayBuffer);
} catch (error) { } catch (error) {
throw new Error(`Error fetching file ${fileUrl}: ${error instanceof Error ? error.message : String(error)}`); throw new Error(
`Error fetching file ${fileUrl}: ${
error instanceof Error ? error.message : String(error)
}`
);
} }
} }
const uri = `data:application/zip;base64,${await zip.generateAsync({ type: "base64" })}`; const uri = `data:application/zip;base64,${await zip.generateAsync({
type: "base64",
})}`;
return { return {
content: [ content: [
@ -932,10 +979,11 @@ export const createServer = () => {
content: [ content: [
{ {
type: "text", type: "text",
text: "The MCP client does not support the roots protocol.\n\n" + text:
"This means the server cannot access information about the client's workspace directories or file system roots." "The MCP client does not support the roots protocol.\n\n" +
} "This means the server cannot access information about the client's workspace directories or file system roots.",
] },
],
}; };
} }
@ -944,29 +992,35 @@ export const createServer = () => {
content: [ content: [
{ {
type: "text", type: "text",
text: "The client supports roots but no roots are currently configured.\n\n" + text:
"The client supports roots but no roots are currently configured.\n\n" +
"This could mean:\n" + "This could mean:\n" +
"1. The client hasn't provided any roots yet\n" + "1. The client hasn't provided any roots yet\n" +
"2. The client provided an empty roots list\n" + "2. The client provided an empty roots list\n" +
"3. The roots configuration is still being loaded" "3. The roots configuration is still being loaded",
} },
] ],
}; };
} }
const rootsList = currentRoots.map((root, index) => { const rootsList = currentRoots
return `${index + 1}. ${root.name || 'Unnamed Root'}\n URI: ${root.uri}`; .map((root, index) => {
}).join('\n\n'); return `${index + 1}. ${root.name || "Unnamed Root"}\n URI: ${
root.uri
}`;
})
.join("\n\n");
return { return {
content: [ content: [
{ {
type: "text", type: "text",
text: `Current MCP Roots (${currentRoots.length} total):\n\n${rootsList}\n\n` + text:
`Current MCP Roots (${currentRoots.length} total):\n\n${rootsList}\n\n` +
"Note: This server demonstrates the roots protocol capability but doesn't actually access files. " + "Note: This server demonstrates the roots protocol capability but doesn't actually access files. " +
"The roots are provided by the MCP client and can be used by servers that need file system access." "The roots are provided by the MCP client and can be used by servers that need file system access.",
} },
] ],
}; };
} }
@ -1003,28 +1057,39 @@ export const createServer = () => {
}); });
// Roots protocol handlers // Roots protocol handlers
server.setNotificationHandler(RootsListChangedNotificationSchema, async () => { server.setNotificationHandler(
RootsListChangedNotificationSchema,
async () => {
try { try {
// Request the updated roots list from the client // Request the updated roots list from the client
const response = await server.listRoots(); const response = await server.listRoots();
if (response && 'roots' in response) { if (response && "roots" in response) {
currentRoots = response.roots; currentRoots = response.roots;
// Log the roots update for demonstration // Log the roots update for demonstration
await server.sendLoggingMessage({ await server.sendLoggingMessage(
{
level: "info", level: "info",
logger: "everything-server", logger: "everything-server",
data: `Roots updated: ${currentRoots.length} root(s) received from client`, data: `Roots updated: ${currentRoots.length} root(s) received from client`,
}, sessionId); },
sessionId
);
} }
} catch (error) { } catch (error) {
await server.sendLoggingMessage({ await server.sendLoggingMessage(
{
level: "error", level: "error",
logger: "everything-server", logger: "everything-server",
data: `Failed to request roots from client: ${error instanceof Error ? error.message : String(error)}`, data: `Failed to request roots from client: ${
}, sessionId); error instanceof Error ? error.message : String(error)
}`,
},
sessionId
);
} }
}); }
);
// Handle post-initialization setup for roots // Handle post-initialization setup for roots
server.oninitialized = async () => { server.oninitialized = async () => {
@ -1034,34 +1099,48 @@ export const createServer = () => {
clientSupportsRoots = true; clientSupportsRoots = true;
try { try {
const response = await server.listRoots(); const response = await server.listRoots();
if (response && 'roots' in response) { if (response && "roots" in response) {
currentRoots = response.roots; currentRoots = response.roots;
await server.sendLoggingMessage({ await server.sendLoggingMessage(
{
level: "info", level: "info",
logger: "everything-server", logger: "everything-server",
data: `Initial roots received: ${currentRoots.length} root(s) from client`, data: `Initial roots received: ${currentRoots.length} root(s) from client`,
}, sessionId); },
sessionId
);
} else { } else {
await server.sendLoggingMessage({ await server.sendLoggingMessage(
{
level: "warning", level: "warning",
logger: "everything-server", logger: "everything-server",
data: "Client returned no roots set", data: "Client returned no roots set",
}, sessionId); },
sessionId
);
} }
} catch (error) { } catch (error) {
await server.sendLoggingMessage({ await server.sendLoggingMessage(
{
level: "error", level: "error",
logger: "everything-server", logger: "everything-server",
data: `Failed to request initial roots from client: ${error instanceof Error ? error.message : String(error)}`, data: `Failed to request initial roots from client: ${
}, sessionId); error instanceof Error ? error.message : String(error)
}`,
},
sessionId
);
} }
} else { } else {
await server.sendLoggingMessage({ await server.sendLoggingMessage(
{
level: "info", level: "info",
logger: "everything-server", logger: "everything-server",
data: "Client does not support MCP roots protocol", data: "Client does not support MCP roots protocol",
}, sessionId); },
sessionId
);
} }
}; };

View file

@ -23,10 +23,10 @@ export const createServer = () => {
prompts: {}, prompts: {},
resources: { resources: {
subscribe: true, subscribe: true,
} },
}, },
instructions, instructions,
}, }
); );
// Register the tools // Register the tools
@ -38,7 +38,7 @@ export const createServer = () => {
return { return {
server, server,
cleanup: () => {}, cleanup: () => {},
startNotificationIntervals: (sessionId?: string) => {} startNotificationIntervals: (sessionId?: string) => {},
}; };
}; };
@ -46,7 +46,10 @@ function readInstructions(): string {
let instructions; let instructions;
try { try {
instructions = readFileSync(join(__dirname, "../instructions.md"), "utf-8"); instructions = readFileSync(
join(__dirname, "..", "docs", "server-instructions.md"),
"utf-8"
);
} catch (e) { } catch (e) {
instructions = "Server instructions not loaded: " + e; instructions = "Server instructions not loaded: " + e;
} }

View file

@ -1,7 +1,6 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { addToolEcho } from "./echo.js"; import { addToolEcho } from "./echo.js";
/** /**
* Register the tools with the MCP server. * Register the tools with the MCP server.
* @param server * @param server

View file

@ -1,27 +1,35 @@
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js"; import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
import express from "express"; import express from "express";
import { createServer } from "../server/index.js"; import { createServer } from "../server/index.js";
import cors from 'cors'; import cors from "cors";
console.error('Starting SSE server...'); console.error("Starting SSE server...");
const app = express(); const app = express();
app.use(cors({ app.use(
"origin": "*", // use "*" with caution in production cors({
"methods": "GET,POST", origin: "*", // use "*" with caution in production
"preflightContinue": false, methods: "GET,POST",
"optionsSuccessStatus": 204, preflightContinue: false,
})); // Enable CORS for all routes so Inspector can connect optionsSuccessStatus: 204,
const transports: Map<string, SSEServerTransport> = new Map<string, SSEServerTransport>(); })
); // Enable CORS for all routes so Inspector can connect
const transports: Map<string, SSEServerTransport> = new Map<
string,
SSEServerTransport
>();
app.get("/sse", async (req, res) => { app.get("/sse", async (req, res) => {
let transport: SSEServerTransport; let transport: SSEServerTransport;
const { server, cleanup, startNotificationIntervals } = createServer(); const { server, cleanup, startNotificationIntervals } = createServer();
if (req?.query?.sessionId) { if (req?.query?.sessionId) {
const sessionId = (req?.query?.sessionId as string); const sessionId = req?.query?.sessionId as string;
transport = transports.get(sessionId) as SSEServerTransport; transport = transports.get(sessionId) as SSEServerTransport;
console.error("Client Reconnecting? This shouldn't happen; when client has a sessionId, GET /sse should not be called again.", transport.sessionId); console.error(
"Client Reconnecting? This shouldn't happen; when client has a sessionId, GET /sse should not be called again.",
transport.sessionId
);
} else { } else {
// Create and store transport for new session // Create and store transport for new session
transport = new SSEServerTransport("/message", res); transport = new SSEServerTransport("/message", res);
@ -40,19 +48,17 @@ app.get("/sse", async (req, res) => {
transports.delete(transport.sessionId); transports.delete(transport.sessionId);
await cleanup(); await cleanup();
}; };
} }
}); });
app.post("/message", async (req, res) => { app.post("/message", async (req, res) => {
const sessionId = (req?.query?.sessionId as string); const sessionId = req?.query?.sessionId as string;
const transport = transports.get(sessionId); const transport = transports.get(sessionId);
if (transport) { if (transport) {
console.error("Client Message from", sessionId); console.error("Client Message from", sessionId);
await transport.handlePostMessage(req, res); await transport.handlePostMessage(req, res);
} else { } else {
console.error(`No transport found for sessionId ${sessionId}`) console.error(`No transport found for sessionId ${sessionId}`);
} }
}); });

View file

@ -15,7 +15,7 @@ app.use(
preflightContinue: false, preflightContinue: false,
optionsSuccessStatus: 204, optionsSuccessStatus: 204,
exposedHeaders: ["mcp-session-id", "last-event-id", "mcp-protocol-version"], exposedHeaders: ["mcp-session-id", "last-event-id", "mcp-protocol-version"],
}), })
); // Enable CORS for all routes so Inspector can connect ); // Enable CORS for all routes so Inspector can connect
const transports: Map<string, StreamableHTTPServerTransport> = new Map< const transports: Map<string, StreamableHTTPServerTransport> = new Map<
@ -55,7 +55,7 @@ app.post("/mcp", async (req: Request, res: Response) => {
const sid = transport.sessionId; const sid = transport.sessionId;
if (sid && transports.has(sid)) { if (sid && transports.has(sid)) {
console.log( console.log(
`Transport closed for session ${sid}, removing from transports map`, `Transport closed for session ${sid}, removing from transports map`
); );
transports.delete(sid); transports.delete(sid);
} }
@ -177,7 +177,7 @@ server.on("error", (err: unknown) => {
: undefined; : undefined;
if (code === "EADDRINUSE") { if (code === "EADDRINUSE") {
console.error( console.error(
`Failed to start: Port ${PORT} is already in use. Set PORT to a free port or stop the conflicting process.`, `Failed to start: Port ${PORT} is already in use. Set PORT to a free port or stop the conflicting process.`
); );
} else { } else {
console.error("HTTP server encountered an error while starting:", err); console.error("HTTP server encountered an error while starting:", err);

View file

@ -4,7 +4,5 @@
"outDir": "./dist", "outDir": "./dist",
"rootDir": "." "rootDir": "."
}, },
"include": [ "include": ["./**/*.ts"]
"./**/*.ts"
]
} }