avancement planning
This commit is contained in:
Generated
Vendored
+5
-5
@@ -1,24 +1,24 @@
|
||||
import { AuthorizationParams, OAuthServerProvider } from '../../server/auth/provider.js';
|
||||
import { OAuthRegisteredClientsStore } from '../../server/auth/clients.js';
|
||||
import { OAuthClientInformationFull, OAuthMetadata, OAuthTokens } from '../../shared/auth.js';
|
||||
import { Response } from "express";
|
||||
import { Response } from 'express';
|
||||
import { AuthInfo } from '../../server/auth/types.js';
|
||||
export declare class DemoInMemoryClientsStore implements OAuthRegisteredClientsStore {
|
||||
private clients;
|
||||
getClient(clientId: string): Promise<{
|
||||
redirect_uris: string[];
|
||||
client_id: string;
|
||||
jwks_uri?: string | undefined;
|
||||
scope?: string | undefined;
|
||||
token_endpoint_auth_method?: string | undefined;
|
||||
grant_types?: string[] | undefined;
|
||||
response_types?: string[] | undefined;
|
||||
client_name?: string | undefined;
|
||||
client_uri?: string | undefined;
|
||||
logo_uri?: string | undefined;
|
||||
scope?: string | undefined;
|
||||
contacts?: string[] | undefined;
|
||||
tos_uri?: string | undefined;
|
||||
policy_uri?: string | undefined;
|
||||
jwks_uri?: string | undefined;
|
||||
jwks?: any;
|
||||
software_id?: string | undefined;
|
||||
software_version?: string | undefined;
|
||||
@@ -30,17 +30,17 @@ export declare class DemoInMemoryClientsStore implements OAuthRegisteredClientsS
|
||||
registerClient(clientMetadata: OAuthClientInformationFull): Promise<{
|
||||
redirect_uris: string[];
|
||||
client_id: string;
|
||||
jwks_uri?: string | undefined;
|
||||
scope?: string | undefined;
|
||||
token_endpoint_auth_method?: string | undefined;
|
||||
grant_types?: string[] | undefined;
|
||||
response_types?: string[] | undefined;
|
||||
client_name?: string | undefined;
|
||||
client_uri?: string | undefined;
|
||||
logo_uri?: string | undefined;
|
||||
scope?: string | undefined;
|
||||
contacts?: string[] | undefined;
|
||||
tos_uri?: string | undefined;
|
||||
policy_uri?: string | undefined;
|
||||
jwks_uri?: string | undefined;
|
||||
jwks?: any;
|
||||
software_id?: string | undefined;
|
||||
software_version?: string | undefined;
|
||||
|
||||
Generated
Vendored
+1
-1
@@ -1 +1 @@
|
||||
{"version":3,"file":"demoInMemoryOAuthProvider.d.ts","sourceRoot":"","sources":["../../../../src/examples/server/demoInMemoryOAuthProvider.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,+BAA+B,CAAC;AACzF,OAAO,EAAE,2BAA2B,EAAE,MAAM,8BAA8B,CAAC;AAC3E,OAAO,EAAE,0BAA0B,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAC9F,OAAgB,EAAW,QAAQ,EAAE,MAAM,SAAS,CAAC;AACrD,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AAKtD,qBAAa,wBAAyB,YAAW,2BAA2B;IAC1E,OAAO,CAAC,OAAO,CAAiD;IAE1D,SAAS,CAAC,QAAQ,EAAE,MAAM;;;;;;;;;;;;;;;;;;;;;;IAI1B,cAAc,CAAC,cAAc,EAAE,0BAA0B;;;;;;;;;;;;;;;;;;;;;;CAIhE;AAED;;;;;;;GAOG;AACH,qBAAa,wBAAyB,YAAW,mBAAmB;IAOtD,OAAO,CAAC,gBAAgB,CAAC;IANrC,YAAY,2BAAkC;IAC9C,OAAO,CAAC,KAAK,CAE4B;IACzC,OAAO,CAAC,MAAM,CAA+B;gBAEzB,gBAAgB,CAAC,GAAE,CAAC,QAAQ,CAAC,EAAE,GAAG,KAAK,OAAO,aAAA;IAE5D,SAAS,CACb,MAAM,EAAE,0BAA0B,EAClC,MAAM,EAAE,mBAAmB,EAC3B,GAAG,EAAE,QAAQ,GACZ,OAAO,CAAC,IAAI,CAAC;IAoBV,6BAA6B,CACjC,MAAM,EAAE,0BAA0B,EAClC,iBAAiB,EAAE,MAAM,GACxB,OAAO,CAAC,MAAM,CAAC;IAWZ,yBAAyB,CAC7B,MAAM,EAAE,0BAA0B,EAClC,iBAAiB,EAAE,MAAM,EAGzB,aAAa,CAAC,E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B,CAAC,GAAE,CAAC,QAAQ,CAAC,EAAE,GAAG,KAAK,OAAO,aAAA;IAE5D,SAAS,CAAC,MAAM,EAAE,0BAA0B,EAAE,MAAM,EAAE,mBAAmB,EAAE,GAAG,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;IAwCxG,6BAA6B,CAAC,MAAM,EAAE,0BAA0B,EAAE,iBAAiB,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAU7G,yBAAyB,CAC3B,MAAM,EAAE,0BAA0B,EAClC,iBAAiB,EAAE,MAAM,EAGzB,aAAa,CAAC,EAAE,MAAM,GACvB,OAAO,CAAC,WAAW,CAAC;IAoCjB,oBAAoB,CACtB,OAAO,EAAE,0BAA0B,EACnC,aAAa,EAAE,MAAM,EACrB,OAAO,CAAC,EAAE,MAAM,EAAE,EAClB,SAAS,CAAC,EAAE,GAAG,GAChB,OAAO,CAAC,WAAW,CAAC;IAIjB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;CAc5D;AAED,eAAO,MAAM,eAAe,oDAIzB;IACC,aAAa,EAAE,GAAG,CAAC;IACnB,YAAY,EAAE,GAAG,CAAC;IAClB,cAAc,EAAE,OAAO,CAAC;CAC3B,KAAG,aA+EH,CAAC"}
|
||||
Generated
Vendored
+39
-17
@@ -1,7 +1,8 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import express from "express";
|
||||
import express from 'express';
|
||||
import { createOAuthMetadata, mcpAuthRouter } from '../../server/auth/router.js';
|
||||
import { resourceUrlFromServerUrl } from '../../shared/auth-utils.js';
|
||||
import { InvalidRequestError } from '../../server/auth/errors.js';
|
||||
export class DemoInMemoryClientsStore {
|
||||
constructor() {
|
||||
this.clients = new Map();
|
||||
@@ -32,7 +33,7 @@ export class DemoInMemoryAuthProvider {
|
||||
async authorize(client, params, res) {
|
||||
const code = randomUUID();
|
||||
const searchParams = new URLSearchParams({
|
||||
code,
|
||||
code
|
||||
});
|
||||
if (params.state !== undefined) {
|
||||
searchParams.set('state', params.state);
|
||||
@@ -41,7 +42,26 @@ export class DemoInMemoryAuthProvider {
|
||||
client,
|
||||
params
|
||||
});
|
||||
const targetUrl = new URL(client.redirect_uris[0]);
|
||||
// Simulate a user login
|
||||
// Set a secure HTTP-only session cookie with authorization info
|
||||
if (res.cookie) {
|
||||
const authCookieData = {
|
||||
userId: 'demo_user',
|
||||
name: 'Demo User',
|
||||
timestamp: Date.now()
|
||||
};
|
||||
res.cookie('demo_session', JSON.stringify(authCookieData), {
|
||||
httpOnly: true,
|
||||
secure: false, // In production, this should be true
|
||||
sameSite: 'lax',
|
||||
maxAge: 24 * 60 * 60 * 1000, // 24 hours - for demo purposes
|
||||
path: '/' // Available to all routes
|
||||
});
|
||||
}
|
||||
if (!client.redirect_uris.includes(params.redirectUri)) {
|
||||
throw new InvalidRequestError('Unregistered redirect_uri');
|
||||
}
|
||||
const targetUrl = new URL(params.redirectUri);
|
||||
targetUrl.search = searchParams.toString();
|
||||
res.redirect(targetUrl.toString());
|
||||
}
|
||||
@@ -75,14 +95,14 @@ export class DemoInMemoryAuthProvider {
|
||||
scopes: codeData.params.scopes || [],
|
||||
expiresAt: Date.now() + 3600000, // 1 hour
|
||||
resource: codeData.params.resource,
|
||||
type: 'access',
|
||||
type: 'access'
|
||||
};
|
||||
this.tokens.set(token, tokenData);
|
||||
return {
|
||||
access_token: token,
|
||||
token_type: 'bearer',
|
||||
expires_in: 3600,
|
||||
scope: (codeData.params.scopes || []).join(' '),
|
||||
scope: (codeData.params.scopes || []).join(' ')
|
||||
};
|
||||
}
|
||||
async exchangeRefreshToken(_client, _refreshToken, _scopes, _resource) {
|
||||
@@ -98,7 +118,7 @@ export class DemoInMemoryAuthProvider {
|
||||
clientId: tokenData.clientId,
|
||||
scopes: tokenData.scopes,
|
||||
expiresAt: Math.floor(tokenData.expiresAt / 1000),
|
||||
resource: tokenData.resource,
|
||||
resource: tokenData.resource
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -108,12 +128,14 @@ export const setupAuthServer = ({ authServerUrl, mcpServerUrl, strictResource })
|
||||
// how to separate an OAuth Authorization Server from a Resource
|
||||
// server in the SDK. The SDK is not intended to be provide a standalone
|
||||
// authorization server.
|
||||
const validateResource = strictResource ? (resource) => {
|
||||
if (!resource)
|
||||
return false;
|
||||
const expectedResource = resourceUrlFromServerUrl(mcpServerUrl);
|
||||
return resource.toString() === expectedResource.toString();
|
||||
} : undefined;
|
||||
const validateResource = strictResource
|
||||
? (resource) => {
|
||||
if (!resource)
|
||||
return false;
|
||||
const expectedResource = resourceUrlFromServerUrl(mcpServerUrl);
|
||||
return resource.toString() === expectedResource.toString();
|
||||
}
|
||||
: undefined;
|
||||
const provider = new DemoInMemoryAuthProvider(validateResource);
|
||||
const authApp = express();
|
||||
authApp.use(express.json());
|
||||
@@ -125,7 +147,7 @@ export const setupAuthServer = ({ authServerUrl, mcpServerUrl, strictResource })
|
||||
authApp.use(mcpAuthRouter({
|
||||
provider,
|
||||
issuerUrl: authServerUrl,
|
||||
scopesSupported: ['mcp:tools'],
|
||||
scopesSupported: ['mcp:tools']
|
||||
}));
|
||||
authApp.post('/introspect', async (req, res) => {
|
||||
try {
|
||||
@@ -140,7 +162,7 @@ export const setupAuthServer = ({ authServerUrl, mcpServerUrl, strictResource })
|
||||
client_id: tokenInfo.clientId,
|
||||
scope: tokenInfo.scopes.join(' '),
|
||||
exp: tokenInfo.expiresAt,
|
||||
aud: tokenInfo.resource,
|
||||
aud: tokenInfo.resource
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -154,7 +176,7 @@ export const setupAuthServer = ({ authServerUrl, mcpServerUrl, strictResource })
|
||||
});
|
||||
const auth_port = authServerUrl.port;
|
||||
// Start the auth server
|
||||
authApp.listen(auth_port, (error) => {
|
||||
authApp.listen(auth_port, error => {
|
||||
if (error) {
|
||||
console.error('Failed to start server:', error);
|
||||
process.exit(1);
|
||||
@@ -166,9 +188,9 @@ export const setupAuthServer = ({ authServerUrl, mcpServerUrl, strictResource })
|
||||
const oauthMetadata = createOAuthMetadata({
|
||||
provider,
|
||||
issuerUrl: authServerUrl,
|
||||
scopesSupported: ['mcp:tools'],
|
||||
scopesSupported: ['mcp:tools']
|
||||
});
|
||||
oauthMetadata.introspection_endpoint = new URL("/introspect", authServerUrl).href;
|
||||
oauthMetadata.introspection_endpoint = new URL('/introspect', authServerUrl).href;
|
||||
return oauthMetadata;
|
||||
};
|
||||
//# sourceMappingURL=demoInMemoryOAuthProvider.js.map
|
||||
Generated
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Generated
Vendored
+39
-40
@@ -1,70 +1,69 @@
|
||||
import express from 'express';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { McpServer } from '../../server/mcp.js';
|
||||
import { StreamableHTTPServerTransport } from '../../server/streamableHttp.js';
|
||||
import { z } from 'zod';
|
||||
import * as z from 'zod/v4';
|
||||
import { isInitializeRequest } from '../../types.js';
|
||||
import cors from 'cors';
|
||||
import { createMcpExpressApp } from '../../server/express.js';
|
||||
// Create an MCP server with implementation details
|
||||
const getServer = () => {
|
||||
const server = new McpServer({
|
||||
name: 'json-response-streamable-http-server',
|
||||
version: '1.0.0',
|
||||
version: '1.0.0'
|
||||
}, {
|
||||
capabilities: {
|
||||
logging: {},
|
||||
logging: {}
|
||||
}
|
||||
});
|
||||
// Register a simple tool that returns a greeting
|
||||
server.tool('greet', 'A simple greeting tool', {
|
||||
name: z.string().describe('Name to greet'),
|
||||
server.registerTool('greet', {
|
||||
description: 'A simple greeting tool',
|
||||
inputSchema: {
|
||||
name: z.string().describe('Name to greet')
|
||||
}
|
||||
}, async ({ name }) => {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Hello, ${name}!`,
|
||||
},
|
||||
],
|
||||
text: `Hello, ${name}!`
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
// Register a tool that sends multiple greetings with notifications
|
||||
server.tool('multi-greet', 'A tool that sends different greetings with delays between them', {
|
||||
name: z.string().describe('Name to greet'),
|
||||
}, async ({ name }, { sendNotification }) => {
|
||||
server.registerTool('multi-greet', {
|
||||
description: 'A tool that sends different greetings with delays between them',
|
||||
inputSchema: {
|
||||
name: z.string().describe('Name to greet')
|
||||
}
|
||||
}, async ({ name }, extra) => {
|
||||
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
||||
await sendNotification({
|
||||
method: "notifications/message",
|
||||
params: { level: "debug", data: `Starting multi-greet for ${name}` }
|
||||
});
|
||||
await server.sendLoggingMessage({
|
||||
level: 'debug',
|
||||
data: `Starting multi-greet for ${name}`
|
||||
}, extra.sessionId);
|
||||
await sleep(1000); // Wait 1 second before first greeting
|
||||
await sendNotification({
|
||||
method: "notifications/message",
|
||||
params: { level: "info", data: `Sending first greeting to ${name}` }
|
||||
});
|
||||
await server.sendLoggingMessage({
|
||||
level: 'info',
|
||||
data: `Sending first greeting to ${name}`
|
||||
}, extra.sessionId);
|
||||
await sleep(1000); // Wait another second before second greeting
|
||||
await sendNotification({
|
||||
method: "notifications/message",
|
||||
params: { level: "info", data: `Sending second greeting to ${name}` }
|
||||
});
|
||||
await server.sendLoggingMessage({
|
||||
level: 'info',
|
||||
data: `Sending second greeting to ${name}`
|
||||
}, extra.sessionId);
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Good morning, ${name}!`,
|
||||
text: `Good morning, ${name}!`
|
||||
}
|
||||
],
|
||||
]
|
||||
};
|
||||
});
|
||||
return server;
|
||||
};
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
// Configure CORS to expose Mcp-Session-Id header for browser-based clients
|
||||
app.use(cors({
|
||||
origin: '*', // Allow all origins - adjust as needed for production
|
||||
exposedHeaders: ['Mcp-Session-Id']
|
||||
}));
|
||||
const app = createMcpExpressApp();
|
||||
// Map to store transports by session ID
|
||||
const transports = {};
|
||||
app.post('/mcp', async (req, res) => {
|
||||
@@ -82,7 +81,7 @@ app.post('/mcp', async (req, res) => {
|
||||
transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: () => randomUUID(),
|
||||
enableJsonResponse: true, // Enable JSON response mode
|
||||
onsessioninitialized: (sessionId) => {
|
||||
onsessioninitialized: sessionId => {
|
||||
// Store the transport by session ID when session is initialized
|
||||
// This avoids race conditions where requests might come in before the session is stored
|
||||
console.log(`Session initialized with ID: ${sessionId}`);
|
||||
@@ -101,9 +100,9 @@ app.post('/mcp', async (req, res) => {
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32000,
|
||||
message: 'Bad Request: No valid session ID provided',
|
||||
message: 'Bad Request: No valid session ID provided'
|
||||
},
|
||||
id: null,
|
||||
id: null
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -117,9 +116,9 @@ app.post('/mcp', async (req, res) => {
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32603,
|
||||
message: 'Internal server error',
|
||||
message: 'Internal server error'
|
||||
},
|
||||
id: null,
|
||||
id: null
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -132,7 +131,7 @@ app.get('/mcp', async (req, res) => {
|
||||
});
|
||||
// Start the server
|
||||
const PORT = 3000;
|
||||
app.listen(PORT, (error) => {
|
||||
app.listen(PORT, error => {
|
||||
if (error) {
|
||||
console.error('Failed to start server:', error);
|
||||
process.exit(1);
|
||||
|
||||
Generated
Vendored
+1
-1
@@ -1 +1 @@
|
||||
{"version":3,"file":"jsonResponseStreamableHttp.js","sourceRoot":"","sources":["../../../../src/examples/server/jsonResponseStreamableHttp.ts"],"names":[],"mappings":"AAAA,OAAO,OAA8B,MAAM,SAAS,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,6BAA6B,EAAE,MAAM,gCAAgC,CAAC;AAC/E,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAC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D,IAAI,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC;YACrB,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;gBACnB,OAAO,EAAE,KAAK;gBACd,KAAK,EAAE;oBACL,IAAI,EAAE,CAAC,KAAK;oBACZ,OAAO,EAAE,uBAAuB;iBACjC;gBACD,EAAE,EAAE,IAAI;aACT,CAAC,CAAC;QACL,CAAC;IACH,CAAC;AACH,CAAC,CAAC,CAAC;AAEH,wDAAwD;AACxD,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,EAAE,GAAY,EAAE,GAAa,EAAE,EAAE;IACpD,qFAAqF;IACrF,kEAAkE;IAClE,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;AAClE,CAAC,CAAC,CAAC;AAEH,mBAAmB;AACnB,MAAM,IAAI,GAAG,IAAI,CAAC;AAClB,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE;IACzB,IAAI,KAAK,EAAE,CAAC;QACV,OAAO,CAAC,KAAK,CAAC,yBAAyB,EAAE,KAAK,CAAC,CAAC;QAChD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;IACD,OAAO,CAAC,GAAG,CAAC,gDAAgD,IAAI,EAAE,CAAC,CAAC;AACtE,CAAC,CAAC,CAAC;AAEH,yBAAyB;AACzB,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,KAAK,IAAI,EAAE;IAC9B,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAC;IACvC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
|
||||
{"version":3,"file":"jsonResponseStreamableHttp.js","sourceRoot":"","sources":["../../../../src/examples/server/jsonResponseStreamableHttp.ts"],"names":[],"mappings":"AAC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AI,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC;YACnB,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;gBACjB,OAAO,EAAE,KAAK;gBACd,KAAK,EAAE;oBACH,IAAI,EAAE,CAAC,KAAK;oBACZ,OAAO,EAAE,uBAAuB;iBACnC;gBACD,EAAE,EAAE,IAAI;aACX,CAAC,CAAC;QACP,CAAC;IACL,CAAC;AACL,CAAC,CAAC,CAAC;AAEH,wDAAwD;AACxD,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,EAAE,GAAY,EAAE,GAAa,EAAE,EAAE;IAClD,qFAAqF;IACrF,kEAAkE;IAClE,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;AACpE,CAAC,CAAC,CAAC;AAEH,mBAAmB;AACnB,MAAM,IAAI,GAAG,IAAI,CAAC;AAClB,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE;IACrB,IAAI,KAAK,EAAE,CAAC;QACR,OAAO,CAAC,KAAK,CAAC,yBAAyB,EAAE,KAAK,CAAC,CAAC;QAChD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACpB,CAAC;IACD,OAAO,CAAC,GAAG,CAAC,gDAAgD,IAAI,EAAE,CAAC,CAAC;AACxE,CAAC,CAAC,CAAC;AAEH,yBAAyB;AACzB,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,KAAK,IAAI,EAAE;IAC5B,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAC;IACvC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACpB,CAAC,CAAC,CAAC"}
|
||||
Generated
Vendored
+22
-20
@@ -3,66 +3,68 @@
|
||||
* Example MCP server using the high-level McpServer API with outputSchema
|
||||
* This demonstrates how to easily create tools with structured output
|
||||
*/
|
||||
import { McpServer } from "../../server/mcp.js";
|
||||
import { StdioServerTransport } from "../../server/stdio.js";
|
||||
import { z } from "zod";
|
||||
import { McpServer } from '../../server/mcp.js';
|
||||
import { StdioServerTransport } from '../../server/stdio.js';
|
||||
import * as z from 'zod/v4';
|
||||
const server = new McpServer({
|
||||
name: "mcp-output-schema-high-level-example",
|
||||
version: "1.0.0",
|
||||
name: 'mcp-output-schema-high-level-example',
|
||||
version: '1.0.0'
|
||||
});
|
||||
// Define a tool with structured output - Weather data
|
||||
server.registerTool("get_weather", {
|
||||
description: "Get weather information for a city",
|
||||
server.registerTool('get_weather', {
|
||||
description: 'Get weather information for a city',
|
||||
inputSchema: {
|
||||
city: z.string().describe("City name"),
|
||||
country: z.string().describe("Country code (e.g., US, UK)")
|
||||
city: z.string().describe('City name'),
|
||||
country: z.string().describe('Country code (e.g., US, UK)')
|
||||
},
|
||||
outputSchema: {
|
||||
temperature: z.object({
|
||||
celsius: z.number(),
|
||||
fahrenheit: z.number()
|
||||
}),
|
||||
conditions: z.enum(["sunny", "cloudy", "rainy", "stormy", "snowy"]),
|
||||
conditions: z.enum(['sunny', 'cloudy', 'rainy', 'stormy', 'snowy']),
|
||||
humidity: z.number().min(0).max(100),
|
||||
wind: z.object({
|
||||
speed_kmh: z.number(),
|
||||
direction: z.string()
|
||||
})
|
||||
},
|
||||
}
|
||||
}, async ({ city, country }) => {
|
||||
// Parameters are available but not used in this example
|
||||
void city;
|
||||
void country;
|
||||
// Simulate weather API call
|
||||
const temp_c = Math.round((Math.random() * 35 - 5) * 10) / 10;
|
||||
const conditions = ["sunny", "cloudy", "rainy", "stormy", "snowy"][Math.floor(Math.random() * 5)];
|
||||
const conditions = ['sunny', 'cloudy', 'rainy', 'stormy', 'snowy'][Math.floor(Math.random() * 5)];
|
||||
const structuredContent = {
|
||||
temperature: {
|
||||
celsius: temp_c,
|
||||
fahrenheit: Math.round((temp_c * 9 / 5 + 32) * 10) / 10
|
||||
fahrenheit: Math.round(((temp_c * 9) / 5 + 32) * 10) / 10
|
||||
},
|
||||
conditions,
|
||||
humidity: Math.round(Math.random() * 100),
|
||||
wind: {
|
||||
speed_kmh: Math.round(Math.random() * 50),
|
||||
direction: ["N", "NE", "E", "SE", "S", "SW", "W", "NW"][Math.floor(Math.random() * 8)]
|
||||
direction: ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'][Math.floor(Math.random() * 8)]
|
||||
}
|
||||
};
|
||||
return {
|
||||
content: [{
|
||||
type: "text",
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: JSON.stringify(structuredContent, null, 2)
|
||||
}],
|
||||
}
|
||||
],
|
||||
structuredContent
|
||||
};
|
||||
});
|
||||
async function main() {
|
||||
const transport = new StdioServerTransport();
|
||||
await server.connect(transport);
|
||||
console.error("High-level Output Schema Example Server running on stdio");
|
||||
console.error('High-level Output Schema Example Server running on stdio');
|
||||
}
|
||||
main().catch((error) => {
|
||||
console.error("Server error:", error);
|
||||
main().catch(error => {
|
||||
console.error('Server error:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
//# sourceMappingURL=mcpServerOutputSchema.js.map
|
||||
Generated
Vendored
+1
-1
@@ -1 +1 @@
|
||||
{"version":3,"file":"mcpServerOutputSchema.js","sourceRoot":"","sources":["../../../../src/examples/server/mcpServerOutputSchema.ts"],"names":[],"mappings":";AACA;;;GAGG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,MAAM,GAAG,IAAI,SAAS,CAC1B;IACE,IAAI,EAAE,sCAAsC;IAC5C,OAAO,EAAE,OAAO;CACjB,CACF,CAAC;AAEF,sDAAsD;AACtD,MAAM,CAAC,YAAY,CACjB,aAAa,EACb;IACE,WAAW,EAAE,oCAAoC;IACjD,WAAW,EAAE;QACX,IAAI,EAAE,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MAAM,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE;SACxD;QACD,UAAU;QACV,QAAQ,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,GAAG,CAAC;QACzC,IAAI,EAAE;YACJ,SAAS,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC;YACzC,SAAS,EAAE,CAAC,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC;SACvF;KACF,CAAC;IAEF,OAAO;QACL,OAAO,EAAE,CAAC;gBACR,IAAI,EAAE,MAAM;gBACZ,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,iBAAiB,EAAE,IAAI,EAAE,CAAC,CAAC;aACjD,CAAC;QACF,iBAAiB;KAClB,CAAC;AACJ,CAAC,CACF,CAAC;AAEF,KAAK,UAAU,IAAI;IACjB,MAAM,SAAS,GAAG,IAAI,oBAAoB,EAAE,CAAC;IAC7C,MAAM,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IAChC,OAAO,CAAC,KAAK,CAAC,0DAA0D,CAAC,CAAC;AAC5E,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;IACrB,OAAO,CAAC,KAAK,CAAC,eAAe,EAAE,KAAK,CAAC,CAAC;IACtC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
|
||||
{"version":3,"file":"mcpServerOutputSchema.js","sourceRoot":"","sources":["../../../../src/examples/server/mcpServerOutputSchema.ts"],"names":[],"mappings":";AACA;;;GAGG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,q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,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE;IACjB,OAAO,CAAC,KAAK,CAAC,eAAe,EAAE,KAAK,CAAC,CAAC;IACtC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACpB,CAAC,CAAC,CAAC"}
|
||||
Generated
Vendored
+23
-27
@@ -1,7 +1,7 @@
|
||||
import express from 'express';
|
||||
import { McpServer } from '../../server/mcp.js';
|
||||
import { SSEServerTransport } from '../../server/sse.js';
|
||||
import { z } from 'zod';
|
||||
import * as z from 'zod/v4';
|
||||
import { createMcpExpressApp } from '../../server/express.js';
|
||||
/**
|
||||
* This example server demonstrates the deprecated HTTP+SSE transport
|
||||
* (protocol version 2024-11-05). It mainly used for testing backward compatible clients.
|
||||
@@ -15,52 +15,48 @@ import { z } from 'zod';
|
||||
const getServer = () => {
|
||||
const server = new McpServer({
|
||||
name: 'simple-sse-server',
|
||||
version: '1.0.0',
|
||||
version: '1.0.0'
|
||||
}, { capabilities: { logging: {} } });
|
||||
server.tool('start-notification-stream', 'Starts sending periodic notifications', {
|
||||
interval: z.number().describe('Interval in milliseconds between notifications').default(1000),
|
||||
count: z.number().describe('Number of notifications to send').default(10),
|
||||
}, async ({ interval, count }, { sendNotification }) => {
|
||||
server.registerTool('start-notification-stream', {
|
||||
description: 'Starts sending periodic notifications',
|
||||
inputSchema: {
|
||||
interval: z.number().describe('Interval in milliseconds between notifications').default(1000),
|
||||
count: z.number().describe('Number of notifications to send').default(10)
|
||||
}
|
||||
}, async ({ interval, count }, extra) => {
|
||||
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
||||
let counter = 0;
|
||||
// Send the initial notification
|
||||
await sendNotification({
|
||||
method: "notifications/message",
|
||||
params: {
|
||||
level: "info",
|
||||
data: `Starting notification stream with ${count} messages every ${interval}ms`
|
||||
}
|
||||
});
|
||||
await server.sendLoggingMessage({
|
||||
level: 'info',
|
||||
data: `Starting notification stream with ${count} messages every ${interval}ms`
|
||||
}, extra.sessionId);
|
||||
// Send periodic notifications
|
||||
while (counter < count) {
|
||||
counter++;
|
||||
await sleep(interval);
|
||||
try {
|
||||
await sendNotification({
|
||||
method: "notifications/message",
|
||||
params: {
|
||||
level: "info",
|
||||
data: `Notification #${counter} at ${new Date().toISOString()}`
|
||||
}
|
||||
});
|
||||
await server.sendLoggingMessage({
|
||||
level: 'info',
|
||||
data: `Notification #${counter} at ${new Date().toISOString()}`
|
||||
}, extra.sessionId);
|
||||
}
|
||||
catch (error) {
|
||||
console.error("Error sending notification:", error);
|
||||
console.error('Error sending notification:', error);
|
||||
}
|
||||
}
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Completed sending ${count} notifications every ${interval}ms`,
|
||||
text: `Completed sending ${count} notifications every ${interval}ms`
|
||||
}
|
||||
],
|
||||
]
|
||||
};
|
||||
});
|
||||
return server;
|
||||
};
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
const app = createMcpExpressApp();
|
||||
// Store transports by session ID
|
||||
const transports = {};
|
||||
// SSE endpoint for establishing the stream
|
||||
@@ -120,7 +116,7 @@ app.post('/messages', async (req, res) => {
|
||||
});
|
||||
// Start the server
|
||||
const PORT = 3000;
|
||||
app.listen(PORT, (error) => {
|
||||
app.listen(PORT, error => {
|
||||
if (error) {
|
||||
console.error('Failed to start server:', error);
|
||||
process.exit(1);
|
||||
|
||||
Generated
Vendored
+1
-1
@@ -1 +1 @@
|
||||
{"version":3,"file":"simpleSseServer.js","sourceRoot":"","sources":["../../../../src/examples/server/simpleSseServer.ts"],"names":[],"mappings":"AAAA,OAAO,OAA8B,MAAM,SAAS,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB;;;;;;;;GAQG;AAEH,gCAAgC;AAChC,MAAM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QAC9E,CAAC;IACL,CAAC;IACD,OAAO,CAAC,GAAG,CAAC,0BAA0B,CAAC,CAAC;IACxC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACpB,CAAC,CAAC,CAAC"}
|
||||
Generated
Vendored
+39
-43
@@ -1,17 +1,19 @@
|
||||
import express from 'express';
|
||||
import { McpServer } from '../../server/mcp.js';
|
||||
import { StreamableHTTPServerTransport } from '../../server/streamableHttp.js';
|
||||
import { z } from 'zod';
|
||||
import cors from 'cors';
|
||||
import * as z from 'zod/v4';
|
||||
import { createMcpExpressApp } from '../../server/express.js';
|
||||
const getServer = () => {
|
||||
// Create an MCP server with implementation details
|
||||
const server = new McpServer({
|
||||
name: 'stateless-streamable-http-server',
|
||||
version: '1.0.0',
|
||||
version: '1.0.0'
|
||||
}, { capabilities: { logging: {} } });
|
||||
// Register a simple prompt
|
||||
server.prompt('greeting-template', 'A simple greeting prompt template', {
|
||||
name: z.string().describe('Name to include in greeting'),
|
||||
server.registerPrompt('greeting-template', {
|
||||
description: 'A simple greeting prompt template',
|
||||
argsSchema: {
|
||||
name: z.string().describe('Name to include in greeting')
|
||||
}
|
||||
}, async ({ name }) => {
|
||||
return {
|
||||
messages: [
|
||||
@@ -19,32 +21,32 @@ const getServer = () => {
|
||||
role: 'user',
|
||||
content: {
|
||||
type: 'text',
|
||||
text: `Please greet ${name} in a friendly manner.`,
|
||||
},
|
||||
},
|
||||
],
|
||||
text: `Please greet ${name} in a friendly manner.`
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
// Register a tool specifically for testing resumability
|
||||
server.tool('start-notification-stream', 'Starts sending periodic notifications for testing resumability', {
|
||||
interval: z.number().describe('Interval in milliseconds between notifications').default(100),
|
||||
count: z.number().describe('Number of notifications to send (0 for 100)').default(10),
|
||||
}, async ({ interval, count }, { sendNotification }) => {
|
||||
server.registerTool('start-notification-stream', {
|
||||
description: 'Starts sending periodic notifications for testing resumability',
|
||||
inputSchema: {
|
||||
interval: z.number().describe('Interval in milliseconds between notifications').default(100),
|
||||
count: z.number().describe('Number of notifications to send (0 for 100)').default(10)
|
||||
}
|
||||
}, async ({ interval, count }, extra) => {
|
||||
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
||||
let counter = 0;
|
||||
while (count === 0 || counter < count) {
|
||||
counter++;
|
||||
try {
|
||||
await sendNotification({
|
||||
method: "notifications/message",
|
||||
params: {
|
||||
level: "info",
|
||||
data: `Periodic notification #${counter} at ${new Date().toISOString()}`
|
||||
}
|
||||
});
|
||||
await server.sendLoggingMessage({
|
||||
level: 'info',
|
||||
data: `Periodic notification #${counter} at ${new Date().toISOString()}`
|
||||
}, extra.sessionId);
|
||||
}
|
||||
catch (error) {
|
||||
console.error("Error sending notification:", error);
|
||||
console.error('Error sending notification:', error);
|
||||
}
|
||||
// Wait for the specified interval
|
||||
await sleep(interval);
|
||||
@@ -53,36 +55,30 @@ const getServer = () => {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Started sending periodic notifications every ${interval}ms`,
|
||||
text: `Started sending periodic notifications every ${interval}ms`
|
||||
}
|
||||
],
|
||||
]
|
||||
};
|
||||
});
|
||||
// Create a simple resource at a fixed URI
|
||||
server.resource('greeting-resource', 'https://example.com/greetings/default', { mimeType: 'text/plain' }, async () => {
|
||||
server.registerResource('greeting-resource', 'https://example.com/greetings/default', { mimeType: 'text/plain' }, async () => {
|
||||
return {
|
||||
contents: [
|
||||
{
|
||||
uri: 'https://example.com/greetings/default',
|
||||
text: 'Hello, world!',
|
||||
},
|
||||
],
|
||||
text: 'Hello, world!'
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
return server;
|
||||
};
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
// Configure CORS to expose Mcp-Session-Id header for browser-based clients
|
||||
app.use(cors({
|
||||
origin: '*', // Allow all origins - adjust as needed for production
|
||||
exposedHeaders: ['Mcp-Session-Id']
|
||||
}));
|
||||
const app = createMcpExpressApp();
|
||||
app.post('/mcp', async (req, res) => {
|
||||
const server = getServer();
|
||||
try {
|
||||
const transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: undefined,
|
||||
sessionIdGenerator: undefined
|
||||
});
|
||||
await server.connect(transport);
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
@@ -99,9 +95,9 @@ app.post('/mcp', async (req, res) => {
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32603,
|
||||
message: 'Internal server error',
|
||||
message: 'Internal server error'
|
||||
},
|
||||
id: null,
|
||||
id: null
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -109,10 +105,10 @@ app.post('/mcp', async (req, res) => {
|
||||
app.get('/mcp', async (req, res) => {
|
||||
console.log('Received GET MCP request');
|
||||
res.writeHead(405).end(JSON.stringify({
|
||||
jsonrpc: "2.0",
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32000,
|
||||
message: "Method not allowed."
|
||||
message: 'Method not allowed.'
|
||||
},
|
||||
id: null
|
||||
}));
|
||||
@@ -120,17 +116,17 @@ app.get('/mcp', async (req, res) => {
|
||||
app.delete('/mcp', async (req, res) => {
|
||||
console.log('Received DELETE MCP request');
|
||||
res.writeHead(405).end(JSON.stringify({
|
||||
jsonrpc: "2.0",
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32000,
|
||||
message: "Method not allowed."
|
||||
message: 'Method not allowed.'
|
||||
},
|
||||
id: null
|
||||
}));
|
||||
});
|
||||
// Start the server
|
||||
const PORT = 3000;
|
||||
app.listen(PORT, (error) => {
|
||||
app.listen(PORT, error => {
|
||||
if (error) {
|
||||
console.error('Failed to start server:', error);
|
||||
process.exit(1);
|
||||
|
||||
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleStatelessStreamableHttp.js.map
Generated
Vendored
+1
-1
@@ -1 +1 @@
|
||||
{"version":3,"file":"simpleStatelessStreamableHttp.js","sourceRoot":"","sources":["../../../../src/examples/server/simpleStateless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,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
|
||||
{"version":3,"file":"simpleStatelessStreamableHttp.js","sourceRoot":"","sources":["../../../../src/examples/server/simpleStatelessStreamableHttp.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,6BAA6B,EAAE,MAAM,gCAAgC,CAAC;AAC/E,OAAO,KAAK,CAAC,MAAM,QAAQ,CAAC;AAE5B,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAE9D,MAAM,SAAS,GAAG,GAAG,EAAE;IACnB,mDAAmD;IACnD,MAAM,MAAM,GAAG,IAAI,SAAS,CACxB;QACI,IAAI,EAAE,kCAAkC;QACxC,OAAO,EAAE,OAAO;KACnB,EACD,EAAE,YAAY,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,EAAE,CACpC,CAAC;IAEF,2BAA2B;IAC3B,MAAM,CAAC,cAAc,CACjB,mBAAmB,EACnB;QACI,WAAW,EAAE,mCAAmC;QAChD,UAAU,EAAE;YACR,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,6BAA6B,CAAC;SAC3D;KACJ,EACD,KAAK,EAAE,EAAE,IAAI,EAAE,EAA4B,EAAE;QACzC,OAAO;YACH,QAAQ,EAAE;gBACN;oBACI,IAAI,EAAE,MAAM;oBACZ,OAAO,EAAE;wBACL,IAAI,EAAE,MAAM;wBACZ,IAAI,EAAE,gBAAgB,IAAI,wBAAwB;qBACrD;iBACJ;aACJ;SACJ,CAAC;IACN,CAAC,CACJ,CAAC;IAEF,wDAAwD;IACxD,MAAM,CAAC,YAAY,CACf,2BAA2B,EAC3B;QACI,WAAW,EAAE,gEAAgE;QAC7E,WAAW,EAAE;YACT,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,gDAAgD,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC;YAC5F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,78 +1,93 @@
|
||||
import express from 'express';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { z } from 'zod';
|
||||
import * as z from 'zod/v4';
|
||||
import { McpServer } from '../../server/mcp.js';
|
||||
import { StreamableHTTPServerTransport } from '../../server/streamableHttp.js';
|
||||
import { getOAuthProtectedResourceMetadataUrl, mcpAuthMetadataRouter } from '../../server/auth/router.js';
|
||||
import { requireBearerAuth } from '../../server/auth/middleware/bearerAuth.js';
|
||||
import { isInitializeRequest } from '../../types.js';
|
||||
import { createMcpExpressApp } from '../../server/express.js';
|
||||
import { ElicitResultSchema, isInitializeRequest } from '../../types.js';
|
||||
import { InMemoryEventStore } from '../shared/inMemoryEventStore.js';
|
||||
import { InMemoryTaskStore, InMemoryTaskMessageQueue } from '../../experimental/tasks/stores/in-memory.js';
|
||||
import { setupAuthServer } from './demoInMemoryOAuthProvider.js';
|
||||
import { checkResourceAllowed } from 'src/shared/auth-utils.js';
|
||||
import cors from 'cors';
|
||||
import { checkResourceAllowed } from '../../shared/auth-utils.js';
|
||||
// Check for OAuth flag
|
||||
const useOAuth = process.argv.includes('--oauth');
|
||||
const strictOAuth = process.argv.includes('--oauth-strict');
|
||||
// Create shared task store for demonstration
|
||||
const taskStore = new InMemoryTaskStore();
|
||||
// Create an MCP server with implementation details
|
||||
const getServer = () => {
|
||||
const server = new McpServer({
|
||||
name: 'simple-streamable-http-server',
|
||||
version: '1.0.0'
|
||||
}, { capabilities: { logging: {} } });
|
||||
version: '1.0.0',
|
||||
icons: [{ src: './mcp.svg', sizes: ['512x512'], mimeType: 'image/svg+xml' }],
|
||||
websiteUrl: 'https://github.com/modelcontextprotocol/typescript-sdk'
|
||||
}, {
|
||||
capabilities: { logging: {}, tasks: { requests: { tools: { call: {} } } } },
|
||||
taskStore, // Enable task support
|
||||
taskMessageQueue: new InMemoryTaskMessageQueue()
|
||||
});
|
||||
// Register a simple tool that returns a greeting
|
||||
server.registerTool('greet', {
|
||||
title: 'Greeting Tool', // Display name for UI
|
||||
description: 'A simple greeting tool',
|
||||
inputSchema: {
|
||||
name: z.string().describe('Name to greet'),
|
||||
},
|
||||
name: z.string().describe('Name to greet')
|
||||
}
|
||||
}, async ({ name }) => {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Hello, ${name}!`,
|
||||
},
|
||||
],
|
||||
text: `Hello, ${name}!`
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
// Register a tool that sends multiple greetings with notifications (with annotations)
|
||||
server.tool('multi-greet', 'A tool that sends different greetings with delays between them', {
|
||||
name: z.string().describe('Name to greet'),
|
||||
}, {
|
||||
title: 'Multiple Greeting Tool',
|
||||
readOnlyHint: true,
|
||||
openWorldHint: false
|
||||
}, async ({ name }, { sendNotification }) => {
|
||||
server.registerTool('multi-greet', {
|
||||
description: 'A tool that sends different greetings with delays between them',
|
||||
inputSchema: {
|
||||
name: z.string().describe('Name to greet')
|
||||
},
|
||||
annotations: {
|
||||
title: 'Multiple Greeting Tool',
|
||||
readOnlyHint: true,
|
||||
openWorldHint: false
|
||||
}
|
||||
}, async ({ name }, extra) => {
|
||||
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
||||
await sendNotification({
|
||||
method: "notifications/message",
|
||||
params: { level: "debug", data: `Starting multi-greet for ${name}` }
|
||||
});
|
||||
await server.sendLoggingMessage({
|
||||
level: 'debug',
|
||||
data: `Starting multi-greet for ${name}`
|
||||
}, extra.sessionId);
|
||||
await sleep(1000); // Wait 1 second before first greeting
|
||||
await sendNotification({
|
||||
method: "notifications/message",
|
||||
params: { level: "info", data: `Sending first greeting to ${name}` }
|
||||
});
|
||||
await server.sendLoggingMessage({
|
||||
level: 'info',
|
||||
data: `Sending first greeting to ${name}`
|
||||
}, extra.sessionId);
|
||||
await sleep(1000); // Wait another second before second greeting
|
||||
await sendNotification({
|
||||
method: "notifications/message",
|
||||
params: { level: "info", data: `Sending second greeting to ${name}` }
|
||||
});
|
||||
await server.sendLoggingMessage({
|
||||
level: 'info',
|
||||
data: `Sending second greeting to ${name}`
|
||||
}, extra.sessionId);
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Good morning, ${name}!`,
|
||||
text: `Good morning, ${name}!`
|
||||
}
|
||||
],
|
||||
]
|
||||
};
|
||||
});
|
||||
// Register a tool that demonstrates elicitation (user input collection)
|
||||
// Register a tool that demonstrates form elicitation (user input collection with a schema)
|
||||
// This creates a closure that captures the server instance
|
||||
server.tool('collect-user-info', 'A tool that collects user information through elicitation', {
|
||||
infoType: z.enum(['contact', 'preferences', 'feedback']).describe('Type of information to collect'),
|
||||
}, async ({ infoType }) => {
|
||||
server.registerTool('collect-user-info', {
|
||||
description: 'A tool that collects user information through form elicitation',
|
||||
inputSchema: {
|
||||
infoType: z.enum(['contact', 'preferences', 'feedback']).describe('Type of information to collect')
|
||||
}
|
||||
}, async ({ infoType }, extra) => {
|
||||
let message;
|
||||
let requestedSchema;
|
||||
switch (infoType) {
|
||||
@@ -84,21 +99,21 @@ const getServer = () => {
|
||||
name: {
|
||||
type: 'string',
|
||||
title: 'Full Name',
|
||||
description: 'Your full name',
|
||||
description: 'Your full name'
|
||||
},
|
||||
email: {
|
||||
type: 'string',
|
||||
title: 'Email Address',
|
||||
description: 'Your email address',
|
||||
format: 'email',
|
||||
format: 'email'
|
||||
},
|
||||
phone: {
|
||||
type: 'string',
|
||||
title: 'Phone Number',
|
||||
description: 'Your phone number (optional)',
|
||||
},
|
||||
description: 'Your phone number (optional)'
|
||||
}
|
||||
},
|
||||
required: ['name', 'email'],
|
||||
required: ['name', 'email']
|
||||
};
|
||||
break;
|
||||
case 'preferences':
|
||||
@@ -111,23 +126,23 @@ const getServer = () => {
|
||||
title: 'Theme',
|
||||
description: 'Choose your preferred theme',
|
||||
enum: ['light', 'dark', 'auto'],
|
||||
enumNames: ['Light', 'Dark', 'Auto'],
|
||||
enumNames: ['Light', 'Dark', 'Auto']
|
||||
},
|
||||
notifications: {
|
||||
type: 'boolean',
|
||||
title: 'Enable Notifications',
|
||||
description: 'Would you like to receive notifications?',
|
||||
default: true,
|
||||
default: true
|
||||
},
|
||||
frequency: {
|
||||
type: 'string',
|
||||
title: 'Notification Frequency',
|
||||
description: 'How often would you like notifications?',
|
||||
enum: ['daily', 'weekly', 'monthly'],
|
||||
enumNames: ['Daily', 'Weekly', 'Monthly'],
|
||||
},
|
||||
enumNames: ['Daily', 'Weekly', 'Monthly']
|
||||
}
|
||||
},
|
||||
required: ['theme'],
|
||||
required: ['theme']
|
||||
};
|
||||
break;
|
||||
case 'feedback':
|
||||
@@ -140,40 +155,44 @@ const getServer = () => {
|
||||
title: 'Rating',
|
||||
description: 'Rate your experience (1-5)',
|
||||
minimum: 1,
|
||||
maximum: 5,
|
||||
maximum: 5
|
||||
},
|
||||
comments: {
|
||||
type: 'string',
|
||||
title: 'Comments',
|
||||
description: 'Additional comments (optional)',
|
||||
maxLength: 500,
|
||||
maxLength: 500
|
||||
},
|
||||
recommend: {
|
||||
type: 'boolean',
|
||||
title: 'Would you recommend this?',
|
||||
description: 'Would you recommend this to others?',
|
||||
},
|
||||
description: 'Would you recommend this to others?'
|
||||
}
|
||||
},
|
||||
required: ['rating', 'recommend'],
|
||||
required: ['rating', 'recommend']
|
||||
};
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unknown info type: ${infoType}`);
|
||||
}
|
||||
try {
|
||||
// Use the underlying server instance to elicit input from the client
|
||||
const result = await server.server.elicitInput({
|
||||
message,
|
||||
requestedSchema,
|
||||
});
|
||||
// Use sendRequest through the extra parameter to elicit input
|
||||
const result = await extra.sendRequest({
|
||||
method: 'elicitation/create',
|
||||
params: {
|
||||
mode: 'form',
|
||||
message,
|
||||
requestedSchema
|
||||
}
|
||||
}, ElicitResultSchema);
|
||||
if (result.action === 'accept') {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Thank you! Collected ${infoType} information: ${JSON.stringify(result.content, null, 2)}`,
|
||||
},
|
||||
],
|
||||
text: `Thank you! Collected ${infoType} information: ${JSON.stringify(result.content, null, 2)}`
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
else if (result.action === 'decline') {
|
||||
@@ -181,9 +200,9 @@ const getServer = () => {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `No information was collected. User declined ${infoType} information request.`,
|
||||
},
|
||||
],
|
||||
text: `No information was collected. User declined ${infoType} information request.`
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
else {
|
||||
@@ -191,9 +210,9 @@ const getServer = () => {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Information collection was cancelled by the user.`,
|
||||
},
|
||||
],
|
||||
text: `Information collection was cancelled by the user.`
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -202,9 +221,9 @@ const getServer = () => {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Error collecting ${infoType} information: ${error}`,
|
||||
},
|
||||
],
|
||||
text: `Error collecting ${infoType} information: ${error}`
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
});
|
||||
@@ -213,8 +232,8 @@ const getServer = () => {
|
||||
title: 'Greeting Template', // Display name for UI
|
||||
description: 'A simple greeting prompt template',
|
||||
argsSchema: {
|
||||
name: z.string().describe('Name to include in greeting'),
|
||||
},
|
||||
name: z.string().describe('Name to include in greeting')
|
||||
}
|
||||
}, async ({ name }) => {
|
||||
return {
|
||||
messages: [
|
||||
@@ -222,32 +241,32 @@ const getServer = () => {
|
||||
role: 'user',
|
||||
content: {
|
||||
type: 'text',
|
||||
text: `Please greet ${name} in a friendly manner.`,
|
||||
},
|
||||
},
|
||||
],
|
||||
text: `Please greet ${name} in a friendly manner.`
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
// Register a tool specifically for testing resumability
|
||||
server.tool('start-notification-stream', 'Starts sending periodic notifications for testing resumability', {
|
||||
interval: z.number().describe('Interval in milliseconds between notifications').default(100),
|
||||
count: z.number().describe('Number of notifications to send (0 for 100)').default(50),
|
||||
}, async ({ interval, count }, { sendNotification }) => {
|
||||
server.registerTool('start-notification-stream', {
|
||||
description: 'Starts sending periodic notifications for testing resumability',
|
||||
inputSchema: {
|
||||
interval: z.number().describe('Interval in milliseconds between notifications').default(100),
|
||||
count: z.number().describe('Number of notifications to send (0 for 100)').default(50)
|
||||
}
|
||||
}, async ({ interval, count }, extra) => {
|
||||
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
||||
let counter = 0;
|
||||
while (count === 0 || counter < count) {
|
||||
counter++;
|
||||
try {
|
||||
await sendNotification({
|
||||
method: "notifications/message",
|
||||
params: {
|
||||
level: "info",
|
||||
data: `Periodic notification #${counter} at ${new Date().toISOString()}`
|
||||
}
|
||||
});
|
||||
await server.sendLoggingMessage({
|
||||
level: 'info',
|
||||
data: `Periodic notification #${counter} at ${new Date().toISOString()}`
|
||||
}, extra.sessionId);
|
||||
}
|
||||
catch (error) {
|
||||
console.error("Error sending notification:", error);
|
||||
console.error('Error sending notification:', error);
|
||||
}
|
||||
// Wait for the specified interval
|
||||
await sleep(interval);
|
||||
@@ -256,9 +275,9 @@ const getServer = () => {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Started sending periodic notifications every ${interval}ms`,
|
||||
text: `Started sending periodic notifications every ${interval}ms`
|
||||
}
|
||||
],
|
||||
]
|
||||
};
|
||||
});
|
||||
// Create a simple resource at a fixed URI
|
||||
@@ -271,9 +290,9 @@ const getServer = () => {
|
||||
contents: [
|
||||
{
|
||||
uri: 'https://example.com/greetings/default',
|
||||
text: 'Hello, world!',
|
||||
},
|
||||
],
|
||||
text: 'Hello, world!'
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
// Create additional resources for ResourceLink demonstration
|
||||
@@ -286,9 +305,9 @@ const getServer = () => {
|
||||
contents: [
|
||||
{
|
||||
uri: 'file:///example/file1.txt',
|
||||
text: 'This is the content of file 1',
|
||||
},
|
||||
],
|
||||
text: 'This is the content of file 1'
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
server.registerResource('example-file-2', 'file:///example/file2.txt', {
|
||||
@@ -300,9 +319,9 @@ const getServer = () => {
|
||||
contents: [
|
||||
{
|
||||
uri: 'file:///example/file2.txt',
|
||||
text: 'This is the content of file 2',
|
||||
},
|
||||
],
|
||||
text: 'This is the content of file 2'
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
// Register a tool that returns ResourceLinks
|
||||
@@ -310,8 +329,8 @@ const getServer = () => {
|
||||
title: 'List Files with ResourceLinks',
|
||||
description: 'Returns a list of files as ResourceLinks without embedding their content',
|
||||
inputSchema: {
|
||||
includeDescriptions: z.boolean().optional().describe('Whether to include descriptions in the resource links'),
|
||||
},
|
||||
includeDescriptions: z.boolean().optional().describe('Whether to include descriptions in the resource links')
|
||||
}
|
||||
}, async ({ includeDescriptions = true }) => {
|
||||
const resourceLinks = [
|
||||
{
|
||||
@@ -340,27 +359,60 @@ const getServer = () => {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: 'Here are the available files as resource links:',
|
||||
text: 'Here are the available files as resource links:'
|
||||
},
|
||||
...resourceLinks,
|
||||
{
|
||||
type: 'text',
|
||||
text: '\nYou can read any of these resources using their URI.',
|
||||
text: '\nYou can read any of these resources using their URI.'
|
||||
}
|
||||
],
|
||||
]
|
||||
};
|
||||
});
|
||||
// Register a long-running tool that demonstrates task execution
|
||||
// Using the experimental tasks API - WARNING: may change without notice
|
||||
server.experimental.tasks.registerToolTask('delay', {
|
||||
title: 'Delay',
|
||||
description: 'A simple tool that delays for a specified duration, useful for testing task execution',
|
||||
inputSchema: {
|
||||
duration: z.number().describe('Duration in milliseconds').default(5000)
|
||||
}
|
||||
}, {
|
||||
async createTask({ duration }, { taskStore, taskRequestedTtl }) {
|
||||
// Create the task
|
||||
const task = await taskStore.createTask({
|
||||
ttl: taskRequestedTtl
|
||||
});
|
||||
// Simulate out-of-band work
|
||||
(async () => {
|
||||
await new Promise(resolve => setTimeout(resolve, duration));
|
||||
await taskStore.storeTaskResult(task.taskId, 'completed', {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Completed ${duration}ms delay`
|
||||
}
|
||||
]
|
||||
});
|
||||
})();
|
||||
// Return CreateTaskResult with the created task
|
||||
return {
|
||||
task
|
||||
};
|
||||
},
|
||||
async getTask(_args, { taskId, taskStore }) {
|
||||
return await taskStore.getTask(taskId);
|
||||
},
|
||||
async getTaskResult(_args, { taskId, taskStore }) {
|
||||
const result = await taskStore.getTaskResult(taskId);
|
||||
return result;
|
||||
}
|
||||
});
|
||||
return server;
|
||||
};
|
||||
const MCP_PORT = process.env.MCP_PORT ? parseInt(process.env.MCP_PORT, 10) : 3000;
|
||||
const AUTH_PORT = process.env.MCP_AUTH_PORT ? parseInt(process.env.MCP_AUTH_PORT, 10) : 3001;
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
// Allow CORS all domains, expose the Mcp-Session-Id header
|
||||
app.use(cors({
|
||||
origin: '*', // Allow all origins
|
||||
exposedHeaders: ["Mcp-Session-Id"]
|
||||
}));
|
||||
const app = createMcpExpressApp();
|
||||
// Set up OAuth if enabled
|
||||
let authMiddleware = null;
|
||||
if (useOAuth) {
|
||||
@@ -377,14 +429,15 @@ if (useOAuth) {
|
||||
const response = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
token: token
|
||||
}).toString()
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Invalid or expired token: ${await response.text()}`);
|
||||
const text = await response.text().catch(() => null);
|
||||
throw new Error(`Invalid or expired token: ${text}`);
|
||||
}
|
||||
const data = await response.json();
|
||||
if (strictOAuth) {
|
||||
@@ -400,7 +453,7 @@ if (useOAuth) {
|
||||
token,
|
||||
clientId: data.client_id,
|
||||
scopes: data.scope ? data.scope.split(' ') : [],
|
||||
expiresAt: data.exp,
|
||||
expiresAt: data.exp
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -409,12 +462,12 @@ if (useOAuth) {
|
||||
oauthMetadata,
|
||||
resourceServerUrl: mcpServerUrl,
|
||||
scopesSupported: ['mcp:tools'],
|
||||
resourceName: 'MCP Demo Server',
|
||||
resourceName: 'MCP Demo Server'
|
||||
}));
|
||||
authMiddleware = requireBearerAuth({
|
||||
verifier: tokenVerifier,
|
||||
requiredScopes: [],
|
||||
resourceMetadataUrl: getOAuthProtectedResourceMetadataUrl(mcpServerUrl),
|
||||
resourceMetadataUrl: getOAuthProtectedResourceMetadataUrl(mcpServerUrl)
|
||||
});
|
||||
}
|
||||
// Map to store transports by session ID
|
||||
@@ -443,7 +496,7 @@ const mcpPostHandler = async (req, res) => {
|
||||
transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: () => randomUUID(),
|
||||
eventStore, // Enable resumability
|
||||
onsessioninitialized: (sessionId) => {
|
||||
onsessioninitialized: sessionId => {
|
||||
// Store the transport by session ID when session is initialized
|
||||
// This avoids race conditions where requests might come in before the session is stored
|
||||
console.log(`Session initialized with ID: ${sessionId}`);
|
||||
@@ -471,9 +524,9 @@ const mcpPostHandler = async (req, res) => {
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32000,
|
||||
message: 'Bad Request: No valid session ID provided',
|
||||
message: 'Bad Request: No valid session ID provided'
|
||||
},
|
||||
id: null,
|
||||
id: null
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -488,9 +541,9 @@ const mcpPostHandler = async (req, res) => {
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32603,
|
||||
message: 'Internal server error',
|
||||
message: 'Internal server error'
|
||||
},
|
||||
id: null,
|
||||
id: null
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -556,7 +609,7 @@ if (useOAuth && authMiddleware) {
|
||||
else {
|
||||
app.delete('/mcp', mcpDeleteHandler);
|
||||
}
|
||||
app.listen(MCP_PORT, (error) => {
|
||||
app.listen(MCP_PORT, error => {
|
||||
if (error) {
|
||||
console.error('Failed to start server:', error);
|
||||
process.exit(1);
|
||||
|
||||
Generated
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Generated
Vendored
+35
-42
@@ -1,16 +1,15 @@
|
||||
import express from 'express';
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { McpServer } from '../../server/mcp.js';
|
||||
import { StreamableHTTPServerTransport } from '../../server/streamableHttp.js';
|
||||
import { SSEServerTransport } from '../../server/sse.js';
|
||||
import { z } from 'zod';
|
||||
import * as z from 'zod/v4';
|
||||
import { isInitializeRequest } from '../../types.js';
|
||||
import { InMemoryEventStore } from '../shared/inMemoryEventStore.js';
|
||||
import cors from 'cors';
|
||||
import { createMcpExpressApp } from '../../server/express.js';
|
||||
/**
|
||||
* This example server demonstrates backwards compatibility with both:
|
||||
* 1. The deprecated HTTP+SSE transport (protocol version 2024-11-05)
|
||||
* 2. The Streamable HTTP transport (protocol version 2025-03-26)
|
||||
* 2. The Streamable HTTP transport (protocol version 2025-11-25)
|
||||
*
|
||||
* It maintains a single MCP server instance but exposes two transport options:
|
||||
* - /mcp: The new Streamable HTTP endpoint (supports GET/POST/DELETE)
|
||||
@@ -20,28 +19,28 @@ import cors from 'cors';
|
||||
const getServer = () => {
|
||||
const server = new McpServer({
|
||||
name: 'backwards-compatible-server',
|
||||
version: '1.0.0',
|
||||
version: '1.0.0'
|
||||
}, { capabilities: { logging: {} } });
|
||||
// Register a simple tool that sends notifications over time
|
||||
server.tool('start-notification-stream', 'Starts sending periodic notifications for testing resumability', {
|
||||
interval: z.number().describe('Interval in milliseconds between notifications').default(100),
|
||||
count: z.number().describe('Number of notifications to send (0 for 100)').default(50),
|
||||
}, async ({ interval, count }, { sendNotification }) => {
|
||||
server.registerTool('start-notification-stream', {
|
||||
description: 'Starts sending periodic notifications for testing resumability',
|
||||
inputSchema: {
|
||||
interval: z.number().describe('Interval in milliseconds between notifications').default(100),
|
||||
count: z.number().describe('Number of notifications to send (0 for 100)').default(50)
|
||||
}
|
||||
}, async ({ interval, count }, extra) => {
|
||||
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
||||
let counter = 0;
|
||||
while (count === 0 || counter < count) {
|
||||
counter++;
|
||||
try {
|
||||
await sendNotification({
|
||||
method: "notifications/message",
|
||||
params: {
|
||||
level: "info",
|
||||
data: `Periodic notification #${counter} at ${new Date().toISOString()}`
|
||||
}
|
||||
});
|
||||
await server.sendLoggingMessage({
|
||||
level: 'info',
|
||||
data: `Periodic notification #${counter} at ${new Date().toISOString()}`
|
||||
}, extra.sessionId);
|
||||
}
|
||||
catch (error) {
|
||||
console.error("Error sending notification:", error);
|
||||
console.error('Error sending notification:', error);
|
||||
}
|
||||
// Wait for the specified interval
|
||||
await sleep(interval);
|
||||
@@ -50,25 +49,19 @@ const getServer = () => {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Started sending periodic notifications every ${interval}ms`,
|
||||
text: `Started sending periodic notifications every ${interval}ms`
|
||||
}
|
||||
],
|
||||
]
|
||||
};
|
||||
});
|
||||
return server;
|
||||
};
|
||||
// Create Express application
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
// Configure CORS to expose Mcp-Session-Id header for browser-based clients
|
||||
app.use(cors({
|
||||
origin: '*', // Allow all origins - adjust as needed for production
|
||||
exposedHeaders: ['Mcp-Session-Id']
|
||||
}));
|
||||
const app = createMcpExpressApp();
|
||||
// Store transports by session ID
|
||||
const transports = {};
|
||||
//=============================================================================
|
||||
// STREAMABLE HTTP TRANSPORT (PROTOCOL VERSION 2025-03-26)
|
||||
// STREAMABLE HTTP TRANSPORT (PROTOCOL VERSION 2025-11-25)
|
||||
//=============================================================================
|
||||
// Handle all MCP Streamable HTTP requests (GET, POST, DELETE) on a single endpoint
|
||||
app.all('/mcp', async (req, res) => {
|
||||
@@ -90,9 +83,9 @@ app.all('/mcp', async (req, res) => {
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32000,
|
||||
message: 'Bad Request: Session exists but uses a different transport protocol',
|
||||
message: 'Bad Request: Session exists but uses a different transport protocol'
|
||||
},
|
||||
id: null,
|
||||
id: null
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -102,7 +95,7 @@ app.all('/mcp', async (req, res) => {
|
||||
transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: () => randomUUID(),
|
||||
eventStore, // Enable resumability
|
||||
onsessioninitialized: (sessionId) => {
|
||||
onsessioninitialized: sessionId => {
|
||||
// Store the transport by session ID when session is initialized
|
||||
console.log(`StreamableHTTP session initialized with ID: ${sessionId}`);
|
||||
transports[sessionId] = transport;
|
||||
@@ -126,9 +119,9 @@ app.all('/mcp', async (req, res) => {
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32000,
|
||||
message: 'Bad Request: No valid session ID provided',
|
||||
message: 'Bad Request: No valid session ID provided'
|
||||
},
|
||||
id: null,
|
||||
id: null
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -142,9 +135,9 @@ app.all('/mcp', async (req, res) => {
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32603,
|
||||
message: 'Internal server error',
|
||||
message: 'Internal server error'
|
||||
},
|
||||
id: null,
|
||||
id: null
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -156,13 +149,13 @@ app.get('/sse', async (req, res) => {
|
||||
console.log('Received GET request to /sse (deprecated SSE transport)');
|
||||
const transport = new SSEServerTransport('/messages', res);
|
||||
transports[transport.sessionId] = transport;
|
||||
res.on("close", () => {
|
||||
res.on('close', () => {
|
||||
delete transports[transport.sessionId];
|
||||
});
|
||||
const server = getServer();
|
||||
await server.connect(transport);
|
||||
});
|
||||
app.post("/messages", async (req, res) => {
|
||||
app.post('/messages', async (req, res) => {
|
||||
const sessionId = req.query.sessionId;
|
||||
let transport;
|
||||
const existingTransport = transports[sessionId];
|
||||
@@ -176,9 +169,9 @@ app.post("/messages", async (req, res) => {
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32000,
|
||||
message: 'Bad Request: Session exists but uses a different transport protocol',
|
||||
message: 'Bad Request: Session exists but uses a different transport protocol'
|
||||
},
|
||||
id: null,
|
||||
id: null
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -191,7 +184,7 @@ app.post("/messages", async (req, res) => {
|
||||
});
|
||||
// Start the server
|
||||
const PORT = 3000;
|
||||
app.listen(PORT, (error) => {
|
||||
app.listen(PORT, error => {
|
||||
if (error) {
|
||||
console.error('Failed to start server:', error);
|
||||
process.exit(1);
|
||||
@@ -201,10 +194,10 @@ app.listen(PORT, (error) => {
|
||||
==============================================
|
||||
SUPPORTED TRANSPORT OPTIONS:
|
||||
|
||||
1. Streamable Http(Protocol version: 2025-03-26)
|
||||
1. Streamable Http(Protocol version: 2025-11-25)
|
||||
Endpoint: /mcp
|
||||
Methods: GET, POST, DELETE
|
||||
Usage:
|
||||
Usage:
|
||||
- Initialize with POST to /mcp
|
||||
- Establish SSE stream with GET to /mcp
|
||||
- Send requests with POST to /mcp
|
||||
|
||||
Generated
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Generated
Vendored
+41
-30
@@ -1,30 +1,38 @@
|
||||
import express from 'express';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { McpServer } from '../../server/mcp.js';
|
||||
import { StreamableHTTPServerTransport } from '../../server/streamableHttp.js';
|
||||
import { isInitializeRequest } from '../../types.js';
|
||||
// Create an MCP server with implementation details
|
||||
const server = new McpServer({
|
||||
name: 'resource-list-changed-notification-server',
|
||||
version: '1.0.0',
|
||||
});
|
||||
import { createMcpExpressApp } from '../../server/express.js';
|
||||
// Factory to create a new MCP server per session.
|
||||
// Each session needs its own server+transport pair to avoid cross-session contamination.
|
||||
const getServer = () => {
|
||||
const server = new McpServer({
|
||||
name: 'resource-list-changed-notification-server',
|
||||
version: '1.0.0'
|
||||
});
|
||||
const addResource = (name, content) => {
|
||||
const uri = `https://mcp-example.com/dynamic/${encodeURIComponent(name)}`;
|
||||
server.registerResource(name, uri, { mimeType: 'text/plain', description: `Dynamic resource: ${name}` }, async () => {
|
||||
return {
|
||||
contents: [{ uri, text: content }]
|
||||
};
|
||||
});
|
||||
};
|
||||
addResource('example-resource', 'Initial content for example-resource');
|
||||
// Periodically add new resources to demonstrate notifications
|
||||
const resourceChangeInterval = setInterval(() => {
|
||||
const name = randomUUID();
|
||||
addResource(name, `Content for ${name}`);
|
||||
}, 5000);
|
||||
// Clean up the interval when the server closes
|
||||
server.server.onclose = () => {
|
||||
clearInterval(resourceChangeInterval);
|
||||
};
|
||||
return server;
|
||||
};
|
||||
// Store transports by session ID to send notifications
|
||||
const transports = {};
|
||||
const addResource = (name, content) => {
|
||||
const uri = `https://mcp-example.com/dynamic/${encodeURIComponent(name)}`;
|
||||
server.resource(name, uri, { mimeType: 'text/plain', description: `Dynamic resource: ${name}` }, async () => {
|
||||
return {
|
||||
contents: [{ uri, text: content }],
|
||||
};
|
||||
});
|
||||
};
|
||||
addResource('example-resource', 'Initial content for example-resource');
|
||||
const resourceChangeInterval = setInterval(() => {
|
||||
const name = randomUUID();
|
||||
addResource(name, `Content for ${name}`);
|
||||
}, 5000); // Change resources every 5 seconds for testing
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
const app = createMcpExpressApp();
|
||||
app.post('/mcp', async (req, res) => {
|
||||
console.log('Received MCP request:', req.body);
|
||||
try {
|
||||
@@ -39,14 +47,15 @@ app.post('/mcp', async (req, res) => {
|
||||
// New initialization request
|
||||
transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: () => randomUUID(),
|
||||
onsessioninitialized: (sessionId) => {
|
||||
onsessioninitialized: sessionId => {
|
||||
// Store the transport by session ID when session is initialized
|
||||
// This avoids race conditions where requests might come in before the session is stored
|
||||
console.log(`Session initialized with ID: ${sessionId}`);
|
||||
transports[sessionId] = transport;
|
||||
}
|
||||
});
|
||||
// Connect the transport to the MCP server
|
||||
// Create a new server per session and connect it to the transport
|
||||
const server = getServer();
|
||||
await server.connect(transport);
|
||||
// Handle the request - the onsessioninitialized callback will store the transport
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
@@ -58,9 +67,9 @@ app.post('/mcp', async (req, res) => {
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32000,
|
||||
message: 'Bad Request: No valid session ID provided',
|
||||
message: 'Bad Request: No valid session ID provided'
|
||||
},
|
||||
id: null,
|
||||
id: null
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -74,9 +83,9 @@ app.post('/mcp', async (req, res) => {
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32603,
|
||||
message: 'Internal server error',
|
||||
message: 'Internal server error'
|
||||
},
|
||||
id: null,
|
||||
id: null
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -94,7 +103,7 @@ app.get('/mcp', async (req, res) => {
|
||||
});
|
||||
// Start the server
|
||||
const PORT = 3000;
|
||||
app.listen(PORT, (error) => {
|
||||
app.listen(PORT, error => {
|
||||
if (error) {
|
||||
console.error('Failed to start server:', error);
|
||||
process.exit(1);
|
||||
@@ -104,8 +113,10 @@ app.listen(PORT, (error) => {
|
||||
// Handle server shutdown
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('Shutting down server...');
|
||||
clearInterval(resourceChangeInterval);
|
||||
await server.close();
|
||||
for (const sessionId in transports) {
|
||||
await transports[sessionId].close();
|
||||
delete transports[sessionId];
|
||||
}
|
||||
process.exit(0);
|
||||
});
|
||||
//# sourceMappingURL=standaloneSseWithGetStreamableHttp.js.map
|
||||
Generated
Vendored
+1
-1
@@ -1 +1 @@
|
||||
{"version":3,"file":"standaloneSseWithGetStreamableHttp.js","sourceRoot":"","sources":["../../../../src/examples/server/standaloneSseWithGetStreamableHttp.ts"],"names":[],"mappings":"AAAA,OAAO,OAA8B,MA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CAAC;QACV,OAAO,CAAC,KAAK,CAAC,yBAAyB,EAAE,KAAK,CAAC,CAAC;QAChD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;IACD,OAAO,CAAC,GAAG,CAAC,4BAA4B,IAAI,EAAE,CAAC,CAAC;AAClD,CAAC,CAAC,CAAC;AAEH,yBAAyB;AACzB,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,KAAK,IAAI,EAAE;IAC9B,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAC;IACvC,aAAa,CAAC,sBAAsB,CAAC,CAAC;IACtC,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC;IACrB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
|
||||
{"version":3,"file":"standaloneSseWithGetStreamableHttp.js","sourceRoot":"","sources":["../../../../src/examples/server/standaloneSseWithGetStreamableHttp.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,6BAA6B,EAAE,MAAM,gCAAgC,CAAC;AAC/E,OAAO,EAAE,mBAAmB,EAAsB,MAAM,gBAAgB,CAAC;AACzE,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAE9D,kDAAkD;AAClD,yFAAyF;AACzF,MAAM,SAAS,GAAG,GAAG,EAAE;IACnB,MAAM,MAAM,GAAG,IAAI,SAAS,CAAC;QACzB,IAAI,EAAE,2CAA2C;QACjD,OAAO,EAAE,OAAO;KACnB,CAAC,CAAC;IAEH,MAAM,WAAW,GAAG,CAAC,IAAY,EAAE,OAAe,EAAE,EAAE;QAClD,MAAM,GAAG,GAAG,mCAAmC,kBAAkB,CAAC,IAAI,CAAC,EAAE,CAAC;QAC1E,MAAM,CAAC,gBAAgB,CACnB,IAAI,EACJ,GAAG,EACH,EA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UAAU,CAAC,SAAS,CAAC,CAAC,KAAK,EAAE,CAAC;QACpC,OAAO,UAAU,CAAC,SAAS,CAAC,CAAC;IACjC,CAAC;IACD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACpB,CAAC,CAAC,CAAC"}
|
||||
Generated
Vendored
+23
-22
@@ -1,47 +1,48 @@
|
||||
// Run with: npx tsx src/examples/server/toolWithSampleServer.ts
|
||||
import { McpServer } from "../../server/mcp.js";
|
||||
import { StdioServerTransport } from "../../server/stdio.js";
|
||||
import { z } from "zod";
|
||||
import { McpServer } from '../../server/mcp.js';
|
||||
import { StdioServerTransport } from '../../server/stdio.js';
|
||||
import * as z from 'zod/v4';
|
||||
const mcpServer = new McpServer({
|
||||
name: "tools-with-sample-server",
|
||||
version: "1.0.0",
|
||||
name: 'tools-with-sample-server',
|
||||
version: '1.0.0'
|
||||
});
|
||||
// Tool that uses LLM sampling to summarize any text
|
||||
mcpServer.registerTool("summarize", {
|
||||
description: "Summarize any text using an LLM",
|
||||
mcpServer.registerTool('summarize', {
|
||||
description: 'Summarize any text using an LLM',
|
||||
inputSchema: {
|
||||
text: z.string().describe("Text to summarize"),
|
||||
},
|
||||
text: z.string().describe('Text to summarize')
|
||||
}
|
||||
}, async ({ text }) => {
|
||||
// Call the LLM through MCP sampling
|
||||
const response = await mcpServer.server.createMessage({
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
role: 'user',
|
||||
content: {
|
||||
type: "text",
|
||||
text: `Please summarize the following text concisely:\n\n${text}`,
|
||||
},
|
||||
},
|
||||
type: 'text',
|
||||
text: `Please summarize the following text concisely:\n\n${text}`
|
||||
}
|
||||
}
|
||||
],
|
||||
maxTokens: 500,
|
||||
maxTokens: 500
|
||||
});
|
||||
// Since we're not passing tools param to createMessage, response.content is single content
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: response.content.type === "text" ? response.content.text : "Unable to generate summary",
|
||||
},
|
||||
],
|
||||
type: 'text',
|
||||
text: response.content.type === 'text' ? response.content.text : 'Unable to generate summary'
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
async function main() {
|
||||
const transport = new StdioServerTransport();
|
||||
await mcpServer.connect(transport);
|
||||
console.log("MCP server is running...");
|
||||
console.log('MCP server is running...');
|
||||
}
|
||||
main().catch((error) => {
|
||||
console.error("Server error:", error);
|
||||
main().catch(error => {
|
||||
console.error('Server error:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
//# sourceMappingURL=toolWithSampleServer.js.map
|
||||
Generated
Vendored
+1
-1
@@ -1 +1 @@
|
||||
{"version":3,"file":"toolWithSampleServer.js","sourceRoot":"","sources":["../../../../src/examples/server/toolWithSampleServer.ts"],"names":[],"mappings":"AACA,gEAAgE;AAEhE,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,SAAS,GAAG,IAAI,SAAS,CAAC;IAC9B,IAAI,EAAE,0BAA0B;IAChC,OAAO,EAAE,OAAO;CACjB,CAAC,CAAC;AAEH,oDAAoD;AACpD,SAAS,CAAC,YAAY,CACpB,WAAW,EACX;IACE,WAAW,EAAE,iCAAiC;IAC9C,WAAW,EAAE;QACX,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,mBAAmB,CAAC;KAC/C;CACF,EACD,KAAK,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE;IACjB,oCAAoC;IACpC,MAAM,QAAQ,GAAG,MAAM,SAAS,CAAC,MAAM,CAAC,aAAa,CAAC;QACpD,QAAQ,EAAE;YACR;gBACE,IAAI,EAAE,MAAM;gBACZ,OAAO,EAAE;oBACP,IAAI,EAAE,MAAM;oBACZ,IAAI,EAAE,qDAAqD,IAAI,EAAE;iBAClE;aACF;SACF;QACD,SAAS,EAAE,GAAG;KACf,CAAC,CAAC;IAEH,OAAO;QACL,OAAO,EAAE;YACP;gBACE,IAAI,EAAE,MAAM;gBACZ,IAAI,EAAE,QAAQ,CAAC,OAAO,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,4BAA4B;aAC9F;SACF;KACF,CAAC;AACJ,CAAC,CACF,CAAC;AAEF,KAAK,UAAU,IAAI;IACjB,MAAM,SAAS,GAAG,IAAI,oBAAoB,EAAE,CAAC;IAC7C,MAAM,SAAS,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACnC,OAAO,CAAC,GAAG,CAAC,0BAA0B,CAAC,CAAC;AAC1C,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;IACrB,OAAO,CAAC,KAAK,CAAC,eAAe,EAAE,KAAK,CAAC,CAAC;IACtC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
|
||||
{"version":3,"file":"toolWithSampleServer.js","sourceRoot":"","sources":["../../../../src/examples/server/toolWithSampleServer.ts"],"names":[],"mappings":"AAAA,gEAAgE;AAEhE,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,KAAK,CAAC,MAAM,QAAQ,CAAC;AAE5B,MAAM,SAAS,GAAG,IAAI,SAAS,CAAC;IAC5B,IAAI,EAAE,0BAA0B;IAChC,OAAO,EAAE,OAAO;CACnB,CAAC,CAAC;AAEH,oDAAoD;AACpD,SAAS,CAAC,YAAY,CAClB,WAAW,EACX;IACI,WAAW,EAAE,iCAAiC;IAC9C,WAAW,EAAE;QACT,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,mBAAmB,CAAC;KACjD;CACJ,EACD,KAAK,EAAE,EAAE,IAAI,EAAE,EAAE,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