avancement planning
This commit is contained in:
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);
|
||||
|
||||
Reference in New Issue
Block a user