修改后台权限
This commit is contained in:
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/elicitationUrlExample.d.ts
generated
vendored
Normal file
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/elicitationUrlExample.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=elicitationUrlExample.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/elicitationUrlExample.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/elicitationUrlExample.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"elicitationUrlExample.d.ts","sourceRoot":"","sources":["../../../../src/examples/client/elicitationUrlExample.ts"],"names":[],"mappings":""}
|
||||
678
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/elicitationUrlExample.js
generated
vendored
Normal file
678
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/elicitationUrlExample.js
generated
vendored
Normal file
@@ -0,0 +1,678 @@
|
||||
// Run with: npx tsx src/examples/client/elicitationUrlExample.ts
|
||||
//
|
||||
// This example demonstrates how to use URL elicitation to securely
|
||||
// collect user input in a remote (HTTP) server.
|
||||
// URL elicitation allows servers to prompt the end-user to open a URL in their browser
|
||||
// to collect sensitive information.
|
||||
import { Client } from '../../client/index.js';
|
||||
import { StreamableHTTPClientTransport } from '../../client/streamableHttp.js';
|
||||
import { createInterface } from 'node:readline';
|
||||
import { ListToolsResultSchema, CallToolResultSchema, ElicitRequestSchema, McpError, ErrorCode, UrlElicitationRequiredError, ElicitationCompleteNotificationSchema } from '../../types.js';
|
||||
import { getDisplayName } from '../../shared/metadataUtils.js';
|
||||
import { InMemoryOAuthClientProvider } from './simpleOAuthClientProvider.js';
|
||||
import { UnauthorizedError } from '../../client/auth.js';
|
||||
import { createServer } from 'node:http';
|
||||
// Set up OAuth (required for this example)
|
||||
const OAUTH_CALLBACK_PORT = 8090; // Use different port than auth server (3001)
|
||||
const OAUTH_CALLBACK_URL = `http://localhost:${OAUTH_CALLBACK_PORT}/callback`;
|
||||
let oauthProvider = undefined;
|
||||
console.log('Getting OAuth token...');
|
||||
const clientMetadata = {
|
||||
client_name: 'Elicitation MCP Client',
|
||||
redirect_uris: [OAUTH_CALLBACK_URL],
|
||||
grant_types: ['authorization_code', 'refresh_token'],
|
||||
response_types: ['code'],
|
||||
token_endpoint_auth_method: 'client_secret_post',
|
||||
scope: 'mcp:tools'
|
||||
};
|
||||
oauthProvider = new InMemoryOAuthClientProvider(OAUTH_CALLBACK_URL, clientMetadata, (redirectUrl) => {
|
||||
console.log(`\n🔗 Please open this URL in your browser to authorize:\n ${redirectUrl.toString()}`);
|
||||
});
|
||||
// Create readline interface for user input
|
||||
const readline = createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
let abortCommand = new AbortController();
|
||||
// Global client and transport for interactive commands
|
||||
let client = null;
|
||||
let transport = null;
|
||||
let serverUrl = 'http://localhost:3000/mcp';
|
||||
let sessionId = undefined;
|
||||
let isProcessingCommand = false;
|
||||
let isProcessingElicitations = false;
|
||||
const elicitationQueue = [];
|
||||
let elicitationQueueSignal = null;
|
||||
let elicitationsCompleteSignal = null;
|
||||
// Map to track pending URL elicitations waiting for completion notifications
|
||||
const pendingURLElicitations = new Map();
|
||||
async function main() {
|
||||
console.log('MCP Interactive Client');
|
||||
console.log('=====================');
|
||||
// Connect to server immediately with default settings
|
||||
await connect();
|
||||
// Start the elicitation loop in the background
|
||||
elicitationLoop().catch(error => {
|
||||
console.error('Unexpected error in elicitation loop:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
// Short delay allowing the server to send any SSE elicitations on connection
|
||||
await new Promise(resolve => setTimeout(resolve, 200));
|
||||
// Wait until we are done processing any initial elicitations
|
||||
await waitForElicitationsToComplete();
|
||||
// Print help and start the command loop
|
||||
printHelp();
|
||||
await commandLoop();
|
||||
}
|
||||
async function waitForElicitationsToComplete() {
|
||||
// Wait until the queue is empty and nothing is being processed
|
||||
while (elicitationQueue.length > 0 || isProcessingElicitations) {
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
}
|
||||
}
|
||||
function printHelp() {
|
||||
console.log('\nAvailable commands:');
|
||||
console.log(' connect [url] - Connect to MCP server (default: http://localhost:3000/mcp)');
|
||||
console.log(' disconnect - Disconnect from server');
|
||||
console.log(' terminate-session - Terminate the current session');
|
||||
console.log(' reconnect - Reconnect to the server');
|
||||
console.log(' list-tools - List available tools');
|
||||
console.log(' call-tool <name> [args] - Call a tool with optional JSON arguments');
|
||||
console.log(' payment-confirm - Test URL elicitation via error response with payment-confirm tool');
|
||||
console.log(' third-party-auth - Test tool that requires third-party OAuth credentials');
|
||||
console.log(' help - Show this help');
|
||||
console.log(' quit - Exit the program');
|
||||
}
|
||||
async function commandLoop() {
|
||||
await new Promise(resolve => {
|
||||
if (!isProcessingElicitations) {
|
||||
resolve();
|
||||
}
|
||||
else {
|
||||
elicitationsCompleteSignal = resolve;
|
||||
}
|
||||
});
|
||||
readline.question('\n> ', { signal: abortCommand.signal }, async (input) => {
|
||||
isProcessingCommand = true;
|
||||
const args = input.trim().split(/\s+/);
|
||||
const command = args[0]?.toLowerCase();
|
||||
try {
|
||||
switch (command) {
|
||||
case 'connect':
|
||||
await connect(args[1]);
|
||||
break;
|
||||
case 'disconnect':
|
||||
await disconnect();
|
||||
break;
|
||||
case 'terminate-session':
|
||||
await terminateSession();
|
||||
break;
|
||||
case 'reconnect':
|
||||
await reconnect();
|
||||
break;
|
||||
case 'list-tools':
|
||||
await listTools();
|
||||
break;
|
||||
case 'call-tool':
|
||||
if (args.length < 2) {
|
||||
console.log('Usage: call-tool <name> [args]');
|
||||
}
|
||||
else {
|
||||
const toolName = args[1];
|
||||
let toolArgs = {};
|
||||
if (args.length > 2) {
|
||||
try {
|
||||
toolArgs = JSON.parse(args.slice(2).join(' '));
|
||||
}
|
||||
catch {
|
||||
console.log('Invalid JSON arguments. Using empty args.');
|
||||
}
|
||||
}
|
||||
await callTool(toolName, toolArgs);
|
||||
}
|
||||
break;
|
||||
case 'payment-confirm':
|
||||
await callPaymentConfirmTool();
|
||||
break;
|
||||
case 'third-party-auth':
|
||||
await callThirdPartyAuthTool();
|
||||
break;
|
||||
case 'help':
|
||||
printHelp();
|
||||
break;
|
||||
case 'quit':
|
||||
case 'exit':
|
||||
await cleanup();
|
||||
return;
|
||||
default:
|
||||
if (command) {
|
||||
console.log(`Unknown command: ${command}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`Error executing command: ${error}`);
|
||||
}
|
||||
finally {
|
||||
isProcessingCommand = false;
|
||||
}
|
||||
// Process another command after we've processed the this one
|
||||
await commandLoop();
|
||||
});
|
||||
}
|
||||
async function elicitationLoop() {
|
||||
while (true) {
|
||||
// Wait until we have elicitations to process
|
||||
await new Promise(resolve => {
|
||||
if (elicitationQueue.length > 0) {
|
||||
resolve();
|
||||
}
|
||||
else {
|
||||
elicitationQueueSignal = resolve;
|
||||
}
|
||||
});
|
||||
isProcessingElicitations = true;
|
||||
abortCommand.abort(); // Abort the command loop if it's running
|
||||
// Process all queued elicitations
|
||||
while (elicitationQueue.length > 0) {
|
||||
const queued = elicitationQueue.shift();
|
||||
console.log(`📤 Processing queued elicitation (${elicitationQueue.length} remaining)`);
|
||||
try {
|
||||
const result = await handleElicitationRequest(queued.request);
|
||||
queued.resolve(result);
|
||||
}
|
||||
catch (error) {
|
||||
queued.reject(error instanceof Error ? error : new Error(String(error)));
|
||||
}
|
||||
}
|
||||
console.log('✅ All queued elicitations processed. Resuming command loop...\n');
|
||||
isProcessingElicitations = false;
|
||||
// Reset the abort controller for the next command loop
|
||||
abortCommand = new AbortController();
|
||||
// Resume the command loop
|
||||
if (elicitationsCompleteSignal) {
|
||||
elicitationsCompleteSignal();
|
||||
elicitationsCompleteSignal = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Enqueues an elicitation request and returns the result.
|
||||
*
|
||||
* This function is used so that our CLI (which can only handle one input request at a time)
|
||||
* can handle elicitation requests and the command loop.
|
||||
*
|
||||
* @param request - The elicitation request to be handled
|
||||
* @returns The elicitation result
|
||||
*/
|
||||
async function elicitationRequestHandler(request) {
|
||||
// If we are processing a command, handle this elicitation immediately
|
||||
if (isProcessingCommand) {
|
||||
console.log('📋 Processing elicitation immediately (during command execution)');
|
||||
return await handleElicitationRequest(request);
|
||||
}
|
||||
// Otherwise, queue the request to be handled by the elicitation loop
|
||||
console.log(`📥 Queueing elicitation request (queue size will be: ${elicitationQueue.length + 1})`);
|
||||
return new Promise((resolve, reject) => {
|
||||
elicitationQueue.push({
|
||||
request,
|
||||
resolve,
|
||||
reject
|
||||
});
|
||||
// Signal the elicitation loop that there's work to do
|
||||
if (elicitationQueueSignal) {
|
||||
elicitationQueueSignal();
|
||||
elicitationQueueSignal = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Handles an elicitation request.
|
||||
*
|
||||
* This function is used to handle the elicitation request and return the result.
|
||||
*
|
||||
* @param request - The elicitation request to be handled
|
||||
* @returns The elicitation result
|
||||
*/
|
||||
async function handleElicitationRequest(request) {
|
||||
const mode = request.params.mode;
|
||||
console.log('\n🔔 Elicitation Request Received:');
|
||||
console.log(`Mode: ${mode}`);
|
||||
if (mode === 'url') {
|
||||
return {
|
||||
action: await handleURLElicitation(request.params)
|
||||
};
|
||||
}
|
||||
else {
|
||||
// Should not happen because the client declares its capabilities to the server,
|
||||
// but being defensive is a good practice:
|
||||
throw new McpError(ErrorCode.InvalidParams, `Unsupported elicitation mode: ${mode}`);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Handles a URL elicitation by opening the URL in the browser.
|
||||
*
|
||||
* Note: This is a shared code for both request handlers and error handlers.
|
||||
* As a result of sharing schema, there is no big forking of logic for the client.
|
||||
*
|
||||
* @param params - The URL elicitation request parameters
|
||||
* @returns The action to take (accept, cancel, or decline)
|
||||
*/
|
||||
async function handleURLElicitation(params) {
|
||||
const url = params.url;
|
||||
const elicitationId = params.elicitationId;
|
||||
const message = params.message;
|
||||
console.log(`🆔 Elicitation ID: ${elicitationId}`); // Print for illustration
|
||||
// Parse URL to show domain for security
|
||||
let domain = 'unknown domain';
|
||||
try {
|
||||
const parsedUrl = new URL(url);
|
||||
domain = parsedUrl.hostname;
|
||||
}
|
||||
catch {
|
||||
console.error('Invalid URL provided by server');
|
||||
return 'decline';
|
||||
}
|
||||
// Example security warning to help prevent phishing attacks
|
||||
console.log('\n⚠️ \x1b[33mSECURITY WARNING\x1b[0m ⚠️');
|
||||
console.log('\x1b[33mThe server is requesting you to open an external URL.\x1b[0m');
|
||||
console.log('\x1b[33mOnly proceed if you trust this server and understand why it needs this.\x1b[0m\n');
|
||||
console.log(`🌐 Target domain: \x1b[36m${domain}\x1b[0m`);
|
||||
console.log(`🔗 Full URL: \x1b[36m${url}\x1b[0m`);
|
||||
console.log(`\nℹ️ Server's reason:\n\n\x1b[36m${message}\x1b[0m\n`);
|
||||
// 1. Ask for user consent to open the URL
|
||||
const consent = await new Promise(resolve => {
|
||||
readline.question('\nDo you want to open this URL in your browser? (y/n): ', input => {
|
||||
resolve(input.trim().toLowerCase());
|
||||
});
|
||||
});
|
||||
// 2. If user did not consent, return appropriate result
|
||||
if (consent === 'no' || consent === 'n') {
|
||||
console.log('❌ URL navigation declined.');
|
||||
return 'decline';
|
||||
}
|
||||
else if (consent !== 'yes' && consent !== 'y') {
|
||||
console.log('🚫 Invalid response. Cancelling elicitation.');
|
||||
return 'cancel';
|
||||
}
|
||||
// 3. Wait for completion notification in the background
|
||||
const completionPromise = new Promise((resolve, reject) => {
|
||||
const timeout = setTimeout(() => {
|
||||
pendingURLElicitations.delete(elicitationId);
|
||||
console.log(`\x1b[31m❌ Elicitation ${elicitationId} timed out waiting for completion.\x1b[0m`);
|
||||
reject(new Error('Elicitation completion timeout'));
|
||||
}, 5 * 60 * 1000); // 5 minute timeout
|
||||
pendingURLElicitations.set(elicitationId, {
|
||||
resolve: () => {
|
||||
clearTimeout(timeout);
|
||||
resolve();
|
||||
},
|
||||
reject,
|
||||
timeout
|
||||
});
|
||||
});
|
||||
completionPromise.catch(error => {
|
||||
console.error('Background completion wait failed:', error);
|
||||
});
|
||||
// 4. Direct user to open the URL in their browser
|
||||
console.log(`\n🔗 Please open this URL in your browser:\n ${url}`);
|
||||
console.log('\n⏳ Waiting for you to complete the interaction in your browser...');
|
||||
console.log(' The server will send a notification once you complete the action.');
|
||||
// 5. Acknowledge the user accepted the elicitation
|
||||
return 'accept';
|
||||
}
|
||||
/**
|
||||
* Example OAuth callback handler - in production, use a more robust approach
|
||||
* for handling callbacks and storing tokens
|
||||
*/
|
||||
/**
|
||||
* Starts a temporary HTTP server to receive the OAuth callback
|
||||
*/
|
||||
async function waitForOAuthCallback() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const server = createServer((req, res) => {
|
||||
// Ignore favicon requests
|
||||
if (req.url === '/favicon.ico') {
|
||||
res.writeHead(404);
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
console.log(`📥 Received callback: ${req.url}`);
|
||||
const parsedUrl = new URL(req.url || '', 'http://localhost');
|
||||
const code = parsedUrl.searchParams.get('code');
|
||||
const error = parsedUrl.searchParams.get('error');
|
||||
if (code) {
|
||||
console.log(`✅ Authorization code received: ${code?.substring(0, 10)}...`);
|
||||
res.writeHead(200, { 'Content-Type': 'text/html' });
|
||||
res.end(`
|
||||
<html>
|
||||
<body>
|
||||
<h1>Authorization Successful!</h1>
|
||||
<p>This simulates successful authorization of the MCP client, which now has an access token for the MCP server.</p>
|
||||
<p>This window will close automatically in 10 seconds.</p>
|
||||
<script>setTimeout(() => window.close(), 10000);</script>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
resolve(code);
|
||||
setTimeout(() => server.close(), 15000);
|
||||
}
|
||||
else if (error) {
|
||||
console.log(`❌ Authorization error: ${error}`);
|
||||
res.writeHead(400, { 'Content-Type': 'text/html' });
|
||||
res.end(`
|
||||
<html>
|
||||
<body>
|
||||
<h1>Authorization Failed</h1>
|
||||
<p>Error: ${error}</p>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
reject(new Error(`OAuth authorization failed: ${error}`));
|
||||
}
|
||||
else {
|
||||
console.log(`❌ No authorization code or error in callback`);
|
||||
res.writeHead(400);
|
||||
res.end('Bad request');
|
||||
reject(new Error('No authorization code provided'));
|
||||
}
|
||||
});
|
||||
server.listen(OAUTH_CALLBACK_PORT, () => {
|
||||
console.log(`OAuth callback server started on http://localhost:${OAUTH_CALLBACK_PORT}`);
|
||||
});
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Attempts to connect to the MCP server with OAuth authentication.
|
||||
* Handles OAuth flow recursively if authorization is required.
|
||||
*/
|
||||
async function attemptConnection(oauthProvider) {
|
||||
console.log('🚢 Creating transport with OAuth provider...');
|
||||
const baseUrl = new URL(serverUrl);
|
||||
transport = new StreamableHTTPClientTransport(baseUrl, {
|
||||
sessionId: sessionId,
|
||||
authProvider: oauthProvider
|
||||
});
|
||||
console.log('🚢 Transport created');
|
||||
try {
|
||||
console.log('🔌 Attempting connection (this will trigger OAuth redirect if needed)...');
|
||||
await client.connect(transport);
|
||||
sessionId = transport.sessionId;
|
||||
console.log('Transport created with session ID:', sessionId);
|
||||
console.log('✅ Connected successfully');
|
||||
}
|
||||
catch (error) {
|
||||
if (error instanceof UnauthorizedError) {
|
||||
console.log('🔐 OAuth required - waiting for authorization...');
|
||||
const callbackPromise = waitForOAuthCallback();
|
||||
const authCode = await callbackPromise;
|
||||
await transport.finishAuth(authCode);
|
||||
console.log('🔐 Authorization code received:', authCode);
|
||||
console.log('🔌 Reconnecting with authenticated transport...');
|
||||
// Recursively retry connection after OAuth completion
|
||||
await attemptConnection(oauthProvider);
|
||||
}
|
||||
else {
|
||||
console.error('❌ Connection failed with non-auth error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
async function connect(url) {
|
||||
if (client) {
|
||||
console.log('Already connected. Disconnect first.');
|
||||
return;
|
||||
}
|
||||
if (url) {
|
||||
serverUrl = url;
|
||||
}
|
||||
console.log(`🔗 Attempting to connect to ${serverUrl}...`);
|
||||
// Create a new client with elicitation capability
|
||||
console.log('👤 Creating MCP client...');
|
||||
client = new Client({
|
||||
name: 'example-client',
|
||||
version: '1.0.0'
|
||||
}, {
|
||||
capabilities: {
|
||||
elicitation: {
|
||||
// Only URL elicitation is supported in this demo
|
||||
// (see server/elicitationExample.ts for a demo of form mode elicitation)
|
||||
url: {}
|
||||
}
|
||||
}
|
||||
});
|
||||
console.log('👤 Client created');
|
||||
// Set up elicitation request handler with proper validation
|
||||
client.setRequestHandler(ElicitRequestSchema, elicitationRequestHandler);
|
||||
// Set up notification handler for elicitation completion
|
||||
client.setNotificationHandler(ElicitationCompleteNotificationSchema, notification => {
|
||||
const { elicitationId } = notification.params;
|
||||
const pending = pendingURLElicitations.get(elicitationId);
|
||||
if (pending) {
|
||||
clearTimeout(pending.timeout);
|
||||
pendingURLElicitations.delete(elicitationId);
|
||||
console.log(`\x1b[32m✅ Elicitation ${elicitationId} completed!\x1b[0m`);
|
||||
pending.resolve();
|
||||
}
|
||||
else {
|
||||
// Shouldn't happen - discard it!
|
||||
console.warn(`Received completion notification for unknown elicitation: ${elicitationId}`);
|
||||
}
|
||||
});
|
||||
try {
|
||||
console.log('🔐 Starting OAuth flow...');
|
||||
await attemptConnection(oauthProvider);
|
||||
console.log('Connected to MCP server');
|
||||
// Set up error handler after connection is established so we don't double log errors
|
||||
client.onerror = error => {
|
||||
console.error('\x1b[31mClient error:', error, '\x1b[0m');
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Failed to connect:', error);
|
||||
client = null;
|
||||
transport = null;
|
||||
return;
|
||||
}
|
||||
}
|
||||
async function disconnect() {
|
||||
if (!client || !transport) {
|
||||
console.log('Not connected.');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await transport.close();
|
||||
console.log('Disconnected from MCP server');
|
||||
client = null;
|
||||
transport = null;
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error disconnecting:', error);
|
||||
}
|
||||
}
|
||||
async function terminateSession() {
|
||||
if (!client || !transport) {
|
||||
console.log('Not connected.');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
console.log('Terminating session with ID:', transport.sessionId);
|
||||
await transport.terminateSession();
|
||||
console.log('Session terminated successfully');
|
||||
// Check if sessionId was cleared after termination
|
||||
if (!transport.sessionId) {
|
||||
console.log('Session ID has been cleared');
|
||||
sessionId = undefined;
|
||||
// Also close the transport and clear client objects
|
||||
await transport.close();
|
||||
console.log('Transport closed after session termination');
|
||||
client = null;
|
||||
transport = null;
|
||||
}
|
||||
else {
|
||||
console.log('Server responded with 405 Method Not Allowed (session termination not supported)');
|
||||
console.log('Session ID is still active:', transport.sessionId);
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error terminating session:', error);
|
||||
}
|
||||
}
|
||||
async function reconnect() {
|
||||
if (client) {
|
||||
await disconnect();
|
||||
}
|
||||
await connect();
|
||||
}
|
||||
async function listTools() {
|
||||
if (!client) {
|
||||
console.log('Not connected to server.');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const toolsRequest = {
|
||||
method: 'tools/list',
|
||||
params: {}
|
||||
};
|
||||
const toolsResult = await client.request(toolsRequest, ListToolsResultSchema);
|
||||
console.log('Available tools:');
|
||||
if (toolsResult.tools.length === 0) {
|
||||
console.log(' No tools available');
|
||||
}
|
||||
else {
|
||||
for (const tool of toolsResult.tools) {
|
||||
console.log(` - id: ${tool.name}, name: ${getDisplayName(tool)}, description: ${tool.description}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.log(`Tools not supported by this server (${error})`);
|
||||
}
|
||||
}
|
||||
async function callTool(name, args) {
|
||||
if (!client) {
|
||||
console.log('Not connected to server.');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const request = {
|
||||
method: 'tools/call',
|
||||
params: {
|
||||
name,
|
||||
arguments: args
|
||||
}
|
||||
};
|
||||
console.log(`Calling tool '${name}' with args:`, args);
|
||||
const result = await client.request(request, CallToolResultSchema);
|
||||
console.log('Tool result:');
|
||||
const resourceLinks = [];
|
||||
result.content.forEach(item => {
|
||||
if (item.type === 'text') {
|
||||
console.log(` ${item.text}`);
|
||||
}
|
||||
else if (item.type === 'resource_link') {
|
||||
const resourceLink = item;
|
||||
resourceLinks.push(resourceLink);
|
||||
console.log(` 📁 Resource Link: ${resourceLink.name}`);
|
||||
console.log(` URI: ${resourceLink.uri}`);
|
||||
if (resourceLink.mimeType) {
|
||||
console.log(` Type: ${resourceLink.mimeType}`);
|
||||
}
|
||||
if (resourceLink.description) {
|
||||
console.log(` Description: ${resourceLink.description}`);
|
||||
}
|
||||
}
|
||||
else if (item.type === 'resource') {
|
||||
console.log(` [Embedded Resource: ${item.resource.uri}]`);
|
||||
}
|
||||
else if (item.type === 'image') {
|
||||
console.log(` [Image: ${item.mimeType}]`);
|
||||
}
|
||||
else if (item.type === 'audio') {
|
||||
console.log(` [Audio: ${item.mimeType}]`);
|
||||
}
|
||||
else {
|
||||
console.log(` [Unknown content type]:`, item);
|
||||
}
|
||||
});
|
||||
// Offer to read resource links
|
||||
if (resourceLinks.length > 0) {
|
||||
console.log(`\nFound ${resourceLinks.length} resource link(s). Use 'read-resource <uri>' to read their content.`);
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
if (error instanceof UrlElicitationRequiredError) {
|
||||
console.log('\n🔔 Elicitation Required Error Received:');
|
||||
console.log(`Message: ${error.message}`);
|
||||
for (const e of error.elicitations) {
|
||||
await handleURLElicitation(e); // For the error handler, we discard the action result because we don't respond to an error response
|
||||
}
|
||||
return;
|
||||
}
|
||||
console.log(`Error calling tool ${name}: ${error}`);
|
||||
}
|
||||
}
|
||||
async function cleanup() {
|
||||
if (client && transport) {
|
||||
try {
|
||||
// First try to terminate the session gracefully
|
||||
if (transport.sessionId) {
|
||||
try {
|
||||
console.log('Terminating session before exit...');
|
||||
await transport.terminateSession();
|
||||
console.log('Session terminated successfully');
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error terminating session:', error);
|
||||
}
|
||||
}
|
||||
// Then close the transport
|
||||
await transport.close();
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error closing transport:', error);
|
||||
}
|
||||
}
|
||||
process.stdin.setRawMode(false);
|
||||
readline.close();
|
||||
console.log('\nGoodbye!');
|
||||
process.exit(0);
|
||||
}
|
||||
async function callPaymentConfirmTool() {
|
||||
console.log('Calling payment-confirm tool...');
|
||||
await callTool('payment-confirm', { cartId: 'cart_123' });
|
||||
}
|
||||
async function callThirdPartyAuthTool() {
|
||||
console.log('Calling third-party-auth tool...');
|
||||
await callTool('third-party-auth', { param1: 'test' });
|
||||
}
|
||||
// Set up raw mode for keyboard input to capture Escape key
|
||||
process.stdin.setRawMode(true);
|
||||
process.stdin.on('data', async (data) => {
|
||||
// Check for Escape key (27)
|
||||
if (data.length === 1 && data[0] === 27) {
|
||||
console.log('\nESC key pressed. Disconnecting from server...');
|
||||
// Abort current operation and disconnect from server
|
||||
if (client && transport) {
|
||||
await disconnect();
|
||||
console.log('Disconnected. Press Enter to continue.');
|
||||
}
|
||||
else {
|
||||
console.log('Not connected to server.');
|
||||
}
|
||||
// Re-display the prompt
|
||||
process.stdout.write('> ');
|
||||
}
|
||||
});
|
||||
// Handle Ctrl+C
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('\nReceived SIGINT. Cleaning up...');
|
||||
await cleanup();
|
||||
});
|
||||
// Start the interactive client
|
||||
main().catch((error) => {
|
||||
console.error('Error running MCP client:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
//# sourceMappingURL=elicitationUrlExample.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/elicitationUrlExample.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/elicitationUrlExample.js.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/multipleClientsParallel.d.ts
generated
vendored
Normal file
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/multipleClientsParallel.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=multipleClientsParallel.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/multipleClientsParallel.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/multipleClientsParallel.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"multipleClientsParallel.d.ts","sourceRoot":"","sources":["../../../../src/examples/client/multipleClientsParallel.ts"],"names":[],"mappings":""}
|
||||
132
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/multipleClientsParallel.js
generated
vendored
Normal file
132
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/multipleClientsParallel.js
generated
vendored
Normal file
@@ -0,0 +1,132 @@
|
||||
import { Client } from '../../client/index.js';
|
||||
import { StreamableHTTPClientTransport } from '../../client/streamableHttp.js';
|
||||
import { CallToolResultSchema, LoggingMessageNotificationSchema } from '../../types.js';
|
||||
/**
|
||||
* Multiple Clients MCP Example
|
||||
*
|
||||
* This client demonstrates how to:
|
||||
* 1. Create multiple MCP clients in parallel
|
||||
* 2. Each client calls a single tool
|
||||
* 3. Track notifications from each client independently
|
||||
*/
|
||||
// Command line args processing
|
||||
const args = process.argv.slice(2);
|
||||
const serverUrl = args[0] || 'http://localhost:3000/mcp';
|
||||
async function createAndRunClient(config) {
|
||||
console.log(`[${config.id}] Creating client: ${config.name}`);
|
||||
const client = new Client({
|
||||
name: config.name,
|
||||
version: '1.0.0'
|
||||
});
|
||||
const transport = new StreamableHTTPClientTransport(new URL(serverUrl));
|
||||
// Set up client-specific error handler
|
||||
client.onerror = error => {
|
||||
console.error(`[${config.id}] Client error:`, error);
|
||||
};
|
||||
// Set up client-specific notification handler
|
||||
client.setNotificationHandler(LoggingMessageNotificationSchema, notification => {
|
||||
console.log(`[${config.id}] Notification: ${notification.params.data}`);
|
||||
});
|
||||
try {
|
||||
// Connect to the server
|
||||
await client.connect(transport);
|
||||
console.log(`[${config.id}] Connected to MCP server`);
|
||||
// Call the specified tool
|
||||
console.log(`[${config.id}] Calling tool: ${config.toolName}`);
|
||||
const toolRequest = {
|
||||
method: 'tools/call',
|
||||
params: {
|
||||
name: config.toolName,
|
||||
arguments: {
|
||||
...config.toolArguments,
|
||||
// Add client ID to arguments for identification in notifications
|
||||
caller: config.id
|
||||
}
|
||||
}
|
||||
};
|
||||
const result = await client.request(toolRequest, CallToolResultSchema);
|
||||
console.log(`[${config.id}] Tool call completed`);
|
||||
// Keep the connection open for a bit to receive notifications
|
||||
await new Promise(resolve => setTimeout(resolve, 5000));
|
||||
// Disconnect
|
||||
await transport.close();
|
||||
console.log(`[${config.id}] Disconnected from MCP server`);
|
||||
return { id: config.id, result };
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`[${config.id}] Error:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
async function main() {
|
||||
console.log('MCP Multiple Clients Example');
|
||||
console.log('============================');
|
||||
console.log(`Server URL: ${serverUrl}`);
|
||||
console.log('');
|
||||
try {
|
||||
// Define client configurations
|
||||
const clientConfigs = [
|
||||
{
|
||||
id: 'client1',
|
||||
name: 'basic-client-1',
|
||||
toolName: 'start-notification-stream',
|
||||
toolArguments: {
|
||||
interval: 3, // 1 second between notifications
|
||||
count: 5 // Send 5 notifications
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 'client2',
|
||||
name: 'basic-client-2',
|
||||
toolName: 'start-notification-stream',
|
||||
toolArguments: {
|
||||
interval: 2, // 2 seconds between notifications
|
||||
count: 3 // Send 3 notifications
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 'client3',
|
||||
name: 'basic-client-3',
|
||||
toolName: 'start-notification-stream',
|
||||
toolArguments: {
|
||||
interval: 1, // 0.5 second between notifications
|
||||
count: 8 // Send 8 notifications
|
||||
}
|
||||
}
|
||||
];
|
||||
// Start all clients in parallel
|
||||
console.log(`Starting ${clientConfigs.length} clients in parallel...`);
|
||||
console.log('');
|
||||
const clientPromises = clientConfigs.map(config => createAndRunClient(config));
|
||||
const results = await Promise.all(clientPromises);
|
||||
// Display results from all clients
|
||||
console.log('\n=== Final Results ===');
|
||||
results.forEach(({ id, result }) => {
|
||||
console.log(`\n[${id}] Tool result:`);
|
||||
if (Array.isArray(result.content)) {
|
||||
result.content.forEach((item) => {
|
||||
if (item.type === 'text' && item.text) {
|
||||
console.log(` ${item.text}`);
|
||||
}
|
||||
else {
|
||||
console.log(` ${item.type} content:`, item);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
console.log(` Unexpected result format:`, result);
|
||||
}
|
||||
});
|
||||
console.log('\n=== All clients completed successfully ===');
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error running multiple clients:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
// Start the example
|
||||
main().catch((error) => {
|
||||
console.error('Error running MCP multiple clients example:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
//# sourceMappingURL=multipleClientsParallel.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/multipleClientsParallel.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/multipleClientsParallel.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"multipleClientsParallel.js","sourceRoot":"","sources":["../../../../src/examples/client/multipleClientsParallel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,6BAA6B,EAAE,MAAM,gCAAgC,CAAC;AAC/E,OAAO,EAAmB,oBAAoB,EAAE,gCAAgC,EAAkB,MAAM,gBAAgB,CAAC;AAE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I,CAAC,IAAI,EAAE,CAAC;wBACpC,OAAO,CAAC,GAAG,CAAC,KAAK,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;oBAClC,CAAC;yBAAM,CAAC;wBACJ,OAAO,CAAC,GAAG,CAAC,KAAK,IAAI,CAAC,IAAI,WAAW,EAAE,IAAI,CAAC,CAAC;oBACjD,CAAC;gBACL,CAAC,CAAC,CAAC;YACP,CAAC;iBAAM,CAAC;gBACJ,OAAO,CAAC,GAAG,CAAC,6BAA6B,EAAE,MAAM,CAAC,CAAC;YACvD,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,OAAO,CAAC,GAAG,CAAC,8CAA8C,CAAC,CAAC;IAChE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,OAAO,CAAC,KAAK,CAAC,iCAAiC,EAAE,KAAK,CAAC,CAAC;QACxD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACpB,CAAC;AACL,CAAC;AAED,oBAAoB;AACpB,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;IAC5B,OAAO,CAAC,KAAK,CAAC,6CAA6C,EAAE,KAAK,CAAC,CAAC;IACpE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACpB,CAAC,CAAC,CAAC"}
|
||||
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/parallelToolCallsClient.d.ts
generated
vendored
Normal file
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/parallelToolCallsClient.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=parallelToolCallsClient.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/parallelToolCallsClient.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/parallelToolCallsClient.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"parallelToolCallsClient.d.ts","sourceRoot":"","sources":["../../../../src/examples/client/parallelToolCallsClient.ts"],"names":[],"mappings":""}
|
||||
174
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/parallelToolCallsClient.js
generated
vendored
Normal file
174
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/parallelToolCallsClient.js
generated
vendored
Normal file
@@ -0,0 +1,174 @@
|
||||
import { Client } from '../../client/index.js';
|
||||
import { StreamableHTTPClientTransport } from '../../client/streamableHttp.js';
|
||||
import { ListToolsResultSchema, CallToolResultSchema, LoggingMessageNotificationSchema } from '../../types.js';
|
||||
/**
|
||||
* Parallel Tool Calls MCP Client
|
||||
*
|
||||
* This client demonstrates how to:
|
||||
* 1. Start multiple tool calls in parallel
|
||||
* 2. Track notifications from each tool call using a caller parameter
|
||||
*/
|
||||
// Command line args processing
|
||||
const args = process.argv.slice(2);
|
||||
const serverUrl = args[0] || 'http://localhost:3000/mcp';
|
||||
async function main() {
|
||||
console.log('MCP Parallel Tool Calls Client');
|
||||
console.log('==============================');
|
||||
console.log(`Connecting to server at: ${serverUrl}`);
|
||||
let client;
|
||||
let transport;
|
||||
try {
|
||||
// Create client with streamable HTTP transport
|
||||
client = new Client({
|
||||
name: 'parallel-tool-calls-client',
|
||||
version: '1.0.0'
|
||||
});
|
||||
client.onerror = error => {
|
||||
console.error('Client error:', error);
|
||||
};
|
||||
// Connect to the server
|
||||
transport = new StreamableHTTPClientTransport(new URL(serverUrl));
|
||||
await client.connect(transport);
|
||||
console.log('Successfully connected to MCP server');
|
||||
// Set up notification handler with caller identification
|
||||
client.setNotificationHandler(LoggingMessageNotificationSchema, notification => {
|
||||
console.log(`Notification: ${notification.params.data}`);
|
||||
});
|
||||
console.log('List tools');
|
||||
const toolsRequest = await listTools(client);
|
||||
console.log('Tools: ', toolsRequest);
|
||||
// 2. Start multiple notification tools in parallel
|
||||
console.log('\n=== Starting Multiple Notification Streams in Parallel ===');
|
||||
const toolResults = await startParallelNotificationTools(client);
|
||||
// Log the results from each tool call
|
||||
for (const [caller, result] of Object.entries(toolResults)) {
|
||||
console.log(`\n=== Tool result for ${caller} ===`);
|
||||
result.content.forEach((item) => {
|
||||
if (item.type === 'text') {
|
||||
console.log(` ${item.text}`);
|
||||
}
|
||||
else {
|
||||
console.log(` ${item.type} content:`, item);
|
||||
}
|
||||
});
|
||||
}
|
||||
// 3. Wait for all notifications (10 seconds)
|
||||
console.log('\n=== Waiting for all notifications ===');
|
||||
await new Promise(resolve => setTimeout(resolve, 10000));
|
||||
// 4. Disconnect
|
||||
console.log('\n=== Disconnecting ===');
|
||||
await transport.close();
|
||||
console.log('Disconnected from MCP server');
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error running client:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* List available tools on the server
|
||||
*/
|
||||
async function listTools(client) {
|
||||
try {
|
||||
const toolsRequest = {
|
||||
method: 'tools/list',
|
||||
params: {}
|
||||
};
|
||||
const toolsResult = await client.request(toolsRequest, ListToolsResultSchema);
|
||||
console.log('Available tools:');
|
||||
if (toolsResult.tools.length === 0) {
|
||||
console.log(' No tools available');
|
||||
}
|
||||
else {
|
||||
for (const tool of toolsResult.tools) {
|
||||
console.log(` - ${tool.name}: ${tool.description}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.log(`Tools not supported by this server: ${error}`);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Start multiple notification tools in parallel with different configurations
|
||||
* Each tool call includes a caller parameter to identify its notifications
|
||||
*/
|
||||
async function startParallelNotificationTools(client) {
|
||||
try {
|
||||
// Define multiple tool calls with different configurations
|
||||
const toolCalls = [
|
||||
{
|
||||
caller: 'fast-notifier',
|
||||
request: {
|
||||
method: 'tools/call',
|
||||
params: {
|
||||
name: 'start-notification-stream',
|
||||
arguments: {
|
||||
interval: 2, // 0.5 second between notifications
|
||||
count: 10, // Send 10 notifications
|
||||
caller: 'fast-notifier' // Identify this tool call
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
caller: 'slow-notifier',
|
||||
request: {
|
||||
method: 'tools/call',
|
||||
params: {
|
||||
name: 'start-notification-stream',
|
||||
arguments: {
|
||||
interval: 5, // 2 seconds between notifications
|
||||
count: 5, // Send 5 notifications
|
||||
caller: 'slow-notifier' // Identify this tool call
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
caller: 'burst-notifier',
|
||||
request: {
|
||||
method: 'tools/call',
|
||||
params: {
|
||||
name: 'start-notification-stream',
|
||||
arguments: {
|
||||
interval: 1, // 0.1 second between notifications
|
||||
count: 3, // Send just 3 notifications
|
||||
caller: 'burst-notifier' // Identify this tool call
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
];
|
||||
console.log(`Starting ${toolCalls.length} notification tools in parallel...`);
|
||||
// Start all tool calls in parallel
|
||||
const toolPromises = toolCalls.map(({ caller, request }) => {
|
||||
console.log(`Starting tool call for ${caller}...`);
|
||||
return client
|
||||
.request(request, CallToolResultSchema)
|
||||
.then(result => ({ caller, result }))
|
||||
.catch(error => {
|
||||
console.error(`Error in tool call for ${caller}:`, error);
|
||||
throw error;
|
||||
});
|
||||
});
|
||||
// Wait for all tool calls to complete
|
||||
const results = await Promise.all(toolPromises);
|
||||
// Organize results by caller
|
||||
const resultsByTool = {};
|
||||
results.forEach(({ caller, result }) => {
|
||||
resultsByTool[caller] = result;
|
||||
});
|
||||
return resultsByTool;
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`Error starting parallel notification tools:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
// Start the client
|
||||
main().catch((error) => {
|
||||
console.error('Error running MCP client:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
//# sourceMappingURL=parallelToolCallsClient.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/parallelToolCallsClient.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/parallelToolCallsClient.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"parallelToolCallsClient.js","sourceRoot":"","sources":["../../../../src/examples/client/parallelToolCallsClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,6BAA6B,EAAE,MAAM,gCAAgC,CAAC;AAC/E,OAAO,EAEH,qBAAqB,EACrB,oBAAoB,EACpB,gCAAgC,EAEnC,MAAM,gBAAgB,CAAC;AAExB;;;;;;GAMG;AAEH,+BAA+B;AAC/B,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACnC,MAAM,SAAS,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,2BAA2B,CAAC;AAEzD,KAAK,UAAU,IAAI;IACf,OAAO,CAAC,GAAG,CAAC,gCAAgC,CAAC,CAAC;IAC9C,OAAO,CAAC,GAAG,CAAC,gCAAgC,CAAC,CAAC;IAC9C,OAAO,CAAC,GAAG,CAAC,4BAA4B,SAAS,EAAE,CAAC,CAAC;IAErD,IAAI,MAAc,CAAC;IACnB,IAAI,SAAwC,CAAC;IAE7C,IAAI,CAAC;QACD,+CAA+C;QAC/C,MAAM,GAAG,IAAI,MAAM,CAAC;YAChB,IAAI,EAAE,4BAA4B;YAClC,OAAO,EAAE,OAAO;SACnB,CAAC,CAAC;QAEH,MAAM,CAAC,OAAO,GAAG,KAAK,CAAC,EAAE;YACrB,OAAO,CAAC,KAAK,CAAC,eAAe,EAAE,KAAK,CAAC,CAAC;QAC1C,CAAC,CAAC;QAEF,wBAAwB;QACxB,SAAS,GAAG,IAAI,6BAA6B,CAAC,IAAI,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC;QAClE,MAAM,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QAChC,OAAO,CAAC,GAAG,CAAC,sCAAsC,CAAC,CAAC;QAEpD,yDAAyD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AC,0BAA0B,MAAM,GAAG,EAAE,KAAK,CAAC,CAAC;gBAC1D,MAAM,KAAK,CAAC;YAChB,CAAC,CAAC,CAAC;QACX,CAAC,CAAC,CAAC;QAEH,sCAAsC;QACtC,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;QAEhD,6BAA6B;QAC7B,MAAM,aAAa,GAAmC,EAAE,CAAC;QACzD,OAAO,CAAC,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE;YACnC,aAAa,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC;QACnC,CAAC,CAAC,CAAC;QAEH,OAAO,aAAa,CAAC;IACzB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,OAAO,CAAC,KAAK,CAAC,6CAA6C,EAAE,KAAK,CAAC,CAAC;QACpE,MAAM,KAAK,CAAC;IAChB,CAAC;AACL,CAAC;AAED,mBAAmB;AACnB,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;IAC5B,OAAO,CAAC,KAAK,CAAC,2BAA2B,EAAE,KAAK,CAAC,CAAC;IAClD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACpB,CAAC,CAAC,CAAC"}
|
||||
20
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleClientCredentials.d.ts
generated
vendored
Normal file
20
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleClientCredentials.d.ts
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Example demonstrating client_credentials grant for machine-to-machine authentication.
|
||||
*
|
||||
* Supports two authentication methods based on environment variables:
|
||||
*
|
||||
* 1. client_secret_basic (default):
|
||||
* MCP_CLIENT_ID - OAuth client ID (required)
|
||||
* MCP_CLIENT_SECRET - OAuth client secret (required)
|
||||
*
|
||||
* 2. private_key_jwt (when MCP_CLIENT_PRIVATE_KEY_PEM is set):
|
||||
* MCP_CLIENT_ID - OAuth client ID (required)
|
||||
* MCP_CLIENT_PRIVATE_KEY_PEM - PEM-encoded private key for JWT signing (required)
|
||||
* MCP_CLIENT_ALGORITHM - Signing algorithm (default: RS256)
|
||||
*
|
||||
* Common:
|
||||
* MCP_SERVER_URL - Server URL (default: http://localhost:3000/mcp)
|
||||
*/
|
||||
export {};
|
||||
//# sourceMappingURL=simpleClientCredentials.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleClientCredentials.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleClientCredentials.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"simpleClientCredentials.d.ts","sourceRoot":"","sources":["../../../../src/examples/client/simpleClientCredentials.ts"],"names":[],"mappings":";AAEA;;;;;;;;;;;;;;;;GAgBG"}
|
||||
68
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleClientCredentials.js
generated
vendored
Normal file
68
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleClientCredentials.js
generated
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Example demonstrating client_credentials grant for machine-to-machine authentication.
|
||||
*
|
||||
* Supports two authentication methods based on environment variables:
|
||||
*
|
||||
* 1. client_secret_basic (default):
|
||||
* MCP_CLIENT_ID - OAuth client ID (required)
|
||||
* MCP_CLIENT_SECRET - OAuth client secret (required)
|
||||
*
|
||||
* 2. private_key_jwt (when MCP_CLIENT_PRIVATE_KEY_PEM is set):
|
||||
* MCP_CLIENT_ID - OAuth client ID (required)
|
||||
* MCP_CLIENT_PRIVATE_KEY_PEM - PEM-encoded private key for JWT signing (required)
|
||||
* MCP_CLIENT_ALGORITHM - Signing algorithm (default: RS256)
|
||||
*
|
||||
* Common:
|
||||
* MCP_SERVER_URL - Server URL (default: http://localhost:3000/mcp)
|
||||
*/
|
||||
import { Client } from '../../client/index.js';
|
||||
import { StreamableHTTPClientTransport } from '../../client/streamableHttp.js';
|
||||
import { ClientCredentialsProvider, PrivateKeyJwtProvider } from '../../client/auth-extensions.js';
|
||||
const DEFAULT_SERVER_URL = process.env.MCP_SERVER_URL || 'http://localhost:3000/mcp';
|
||||
function createProvider() {
|
||||
const clientId = process.env.MCP_CLIENT_ID;
|
||||
if (!clientId) {
|
||||
console.error('MCP_CLIENT_ID environment variable is required');
|
||||
process.exit(1);
|
||||
}
|
||||
// If private key is provided, use private_key_jwt authentication
|
||||
const privateKeyPem = process.env.MCP_CLIENT_PRIVATE_KEY_PEM;
|
||||
if (privateKeyPem) {
|
||||
const algorithm = process.env.MCP_CLIENT_ALGORITHM || 'RS256';
|
||||
console.log('Using private_key_jwt authentication');
|
||||
return new PrivateKeyJwtProvider({
|
||||
clientId,
|
||||
privateKey: privateKeyPem,
|
||||
algorithm
|
||||
});
|
||||
}
|
||||
// Otherwise, use client_secret_basic authentication
|
||||
const clientSecret = process.env.MCP_CLIENT_SECRET;
|
||||
if (!clientSecret) {
|
||||
console.error('MCP_CLIENT_SECRET or MCP_CLIENT_PRIVATE_KEY_PEM environment variable is required');
|
||||
process.exit(1);
|
||||
}
|
||||
console.log('Using client_secret_basic authentication');
|
||||
return new ClientCredentialsProvider({
|
||||
clientId,
|
||||
clientSecret
|
||||
});
|
||||
}
|
||||
async function main() {
|
||||
const provider = createProvider();
|
||||
const client = new Client({ name: 'client-credentials-example', version: '1.0.0' }, { capabilities: {} });
|
||||
const transport = new StreamableHTTPClientTransport(new URL(DEFAULT_SERVER_URL), {
|
||||
authProvider: provider
|
||||
});
|
||||
await client.connect(transport);
|
||||
console.log('Connected successfully.');
|
||||
const tools = await client.listTools();
|
||||
console.log('Available tools:', tools.tools.map(t => t.name).join(', ') || '(none)');
|
||||
await transport.close();
|
||||
}
|
||||
main().catch(err => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
//# sourceMappingURL=simpleClientCredentials.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleClientCredentials.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleClientCredentials.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"simpleClientCredentials.js","sourceRoot":"","sources":["../../../../src/examples/client/simpleClientCredentials.ts"],"names":[],"mappings":";AAEA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,6BAA6B,EAAE,MAAM,gCAAgC,CAAC;AAC/E,OAAO,EAAE,yBAAyB,EAAE,qBAAqB,EAAE,MAAM,iCAAiC,CAAC;AAGnG,MAAM,kBAAkB,GAAG,OAAO,CAAC,GAAG,CAAC,cAAc,IAAI,2BAA2B,CAAC;AAErF,SAAS,cAAc;IACnB,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC;IAC3C,IAAI,CAAC,QAAQ,EAAE,CAAC;QACZ,OAAO,CAAC,KAAK,CAAC,gDAAgD,CAAC,CAAC;QAChE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACpB,CAAC;IAED,iEAAiE;IACjE,MAAM,aAAa,GAAG,OAAO,CAAC,GAAG,CAAC,0BAA0B,CAAC;IAC7D,IAAI,aAAa,EAAE,CAAC;QAChB,MAAM,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC,oBAAoB,IAAI,OAAO,CAAC;QAC9D,OAAO,CAAC,GAAG,CAAC,sCAAsC,CAAC,CAAC;QACpD,OAAO,IAAI,qBAAqB,CAAC;YAC7B,QAAQ;YACR,UAAU,EAAE,aAAa;YACzB,SAAS;SACZ,CAAC,CAAC;IACP,CAAC;IAED,oDAAoD;IACpD,MAAM,YAAY,GAAG,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC;IACnD,IAAI,CAAC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leOAuthClient.d.ts
generated
vendored
Normal file
3
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleOAuthClient.d.ts
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env node
|
||||
export {};
|
||||
//# sourceMappingURL=simpleOAuthClient.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleOAuthClient.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleOAuthClient.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"simpleOAuthClient.d.ts","sourceRoot":"","sources":["../../../../src/examples/client/simpleOAuthClient.ts"],"names":[],"mappings":""}
|
||||
395
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleOAuthClient.js
generated
vendored
Normal file
395
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleOAuthClient.js
generated
vendored
Normal file
@@ -0,0 +1,395 @@
|
||||
#!/usr/bin/env node
|
||||
import { createServer } from 'node:http';
|
||||
import { createInterface } from 'node:readline';
|
||||
import { URL } from 'node:url';
|
||||
import { Client } from '../../client/index.js';
|
||||
import { StreamableHTTPClientTransport } from '../../client/streamableHttp.js';
|
||||
import { CallToolResultSchema, ListToolsResultSchema } from '../../types.js';
|
||||
import { UnauthorizedError } from '../../client/auth.js';
|
||||
import { InMemoryOAuthClientProvider } from './simpleOAuthClientProvider.js';
|
||||
// Configuration
|
||||
const DEFAULT_SERVER_URL = 'http://localhost:3000/mcp';
|
||||
const CALLBACK_PORT = 8090; // Use different port than auth server (3001)
|
||||
const CALLBACK_URL = `http://localhost:${CALLBACK_PORT}/callback`;
|
||||
/**
|
||||
* Interactive MCP client with OAuth authentication
|
||||
* Demonstrates the complete OAuth flow with browser-based authorization
|
||||
*/
|
||||
class InteractiveOAuthClient {
|
||||
constructor(serverUrl, clientMetadataUrl) {
|
||||
this.serverUrl = serverUrl;
|
||||
this.clientMetadataUrl = clientMetadataUrl;
|
||||
this.client = null;
|
||||
this.rl = createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Prompts user for input via readline
|
||||
*/
|
||||
async question(query) {
|
||||
return new Promise(resolve => {
|
||||
this.rl.question(query, resolve);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Example OAuth callback handler - in production, use a more robust approach
|
||||
* for handling callbacks and storing tokens
|
||||
*/
|
||||
/**
|
||||
* Starts a temporary HTTP server to receive the OAuth callback
|
||||
*/
|
||||
async waitForOAuthCallback() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const server = createServer((req, res) => {
|
||||
// Ignore favicon requests
|
||||
if (req.url === '/favicon.ico') {
|
||||
res.writeHead(404);
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
console.log(`📥 Received callback: ${req.url}`);
|
||||
const parsedUrl = new URL(req.url || '', 'http://localhost');
|
||||
const code = parsedUrl.searchParams.get('code');
|
||||
const error = parsedUrl.searchParams.get('error');
|
||||
if (code) {
|
||||
console.log(`✅ Authorization code received: ${code?.substring(0, 10)}...`);
|
||||
res.writeHead(200, { 'Content-Type': 'text/html' });
|
||||
res.end(`
|
||||
<html>
|
||||
<body>
|
||||
<h1>Authorization Successful!</h1>
|
||||
<p>You can close this window and return to the terminal.</p>
|
||||
<script>setTimeout(() => window.close(), 2000);</script>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
resolve(code);
|
||||
setTimeout(() => server.close(), 3000);
|
||||
}
|
||||
else if (error) {
|
||||
console.log(`❌ Authorization error: ${error}`);
|
||||
res.writeHead(400, { 'Content-Type': 'text/html' });
|
||||
res.end(`
|
||||
<html>
|
||||
<body>
|
||||
<h1>Authorization Failed</h1>
|
||||
<p>Error: ${error}</p>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
reject(new Error(`OAuth authorization failed: ${error}`));
|
||||
}
|
||||
else {
|
||||
console.log(`❌ No authorization code or error in callback`);
|
||||
res.writeHead(400);
|
||||
res.end('Bad request');
|
||||
reject(new Error('No authorization code provided'));
|
||||
}
|
||||
});
|
||||
server.listen(CALLBACK_PORT, () => {
|
||||
console.log(`OAuth callback server started on http://localhost:${CALLBACK_PORT}`);
|
||||
});
|
||||
});
|
||||
}
|
||||
async attemptConnection(oauthProvider) {
|
||||
console.log('🚢 Creating transport with OAuth provider...');
|
||||
const baseUrl = new URL(this.serverUrl);
|
||||
const transport = new StreamableHTTPClientTransport(baseUrl, {
|
||||
authProvider: oauthProvider
|
||||
});
|
||||
console.log('🚢 Transport created');
|
||||
try {
|
||||
console.log('🔌 Attempting connection (this will trigger OAuth redirect)...');
|
||||
await this.client.connect(transport);
|
||||
console.log('✅ Connected successfully');
|
||||
}
|
||||
catch (error) {
|
||||
if (error instanceof UnauthorizedError) {
|
||||
console.log('🔐 OAuth required - waiting for authorization...');
|
||||
const callbackPromise = this.waitForOAuthCallback();
|
||||
const authCode = await callbackPromise;
|
||||
await transport.finishAuth(authCode);
|
||||
console.log('🔐 Authorization code received:', authCode);
|
||||
console.log('🔌 Reconnecting with authenticated transport...');
|
||||
await this.attemptConnection(oauthProvider);
|
||||
}
|
||||
else {
|
||||
console.error('❌ Connection failed with non-auth error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Establishes connection to the MCP server with OAuth authentication
|
||||
*/
|
||||
async connect() {
|
||||
console.log(`🔗 Attempting to connect to ${this.serverUrl}...`);
|
||||
const clientMetadata = {
|
||||
client_name: 'Simple OAuth MCP Client',
|
||||
redirect_uris: [CALLBACK_URL],
|
||||
grant_types: ['authorization_code', 'refresh_token'],
|
||||
response_types: ['code'],
|
||||
token_endpoint_auth_method: 'client_secret_post'
|
||||
};
|
||||
console.log('🔐 Creating OAuth provider...');
|
||||
const oauthProvider = new InMemoryOAuthClientProvider(CALLBACK_URL, clientMetadata, (redirectUrl) => {
|
||||
console.log(`\n🔗 Please open this URL in your browser to authorize:\n ${redirectUrl.toString()}`);
|
||||
}, this.clientMetadataUrl);
|
||||
console.log('🔐 OAuth provider created');
|
||||
console.log('👤 Creating MCP client...');
|
||||
this.client = new Client({
|
||||
name: 'simple-oauth-client',
|
||||
version: '1.0.0'
|
||||
}, { capabilities: {} });
|
||||
console.log('👤 Client created');
|
||||
console.log('🔐 Starting OAuth flow...');
|
||||
await this.attemptConnection(oauthProvider);
|
||||
// Start interactive loop
|
||||
await this.interactiveLoop();
|
||||
}
|
||||
/**
|
||||
* Main interactive loop for user commands
|
||||
*/
|
||||
async interactiveLoop() {
|
||||
console.log('\n🎯 Interactive MCP Client with OAuth');
|
||||
console.log('Commands:');
|
||||
console.log(' list - List available tools');
|
||||
console.log(' call <tool_name> [args] - Call a tool');
|
||||
console.log(' stream <tool_name> [args] - Call a tool with streaming (shows task status)');
|
||||
console.log(' quit - Exit the client');
|
||||
console.log();
|
||||
while (true) {
|
||||
try {
|
||||
const command = await this.question('mcp> ');
|
||||
if (!command.trim()) {
|
||||
continue;
|
||||
}
|
||||
if (command === 'quit') {
|
||||
console.log('\n👋 Goodbye!');
|
||||
this.close();
|
||||
process.exit(0);
|
||||
}
|
||||
else if (command === 'list') {
|
||||
await this.listTools();
|
||||
}
|
||||
else if (command.startsWith('call ')) {
|
||||
await this.handleCallTool(command);
|
||||
}
|
||||
else if (command.startsWith('stream ')) {
|
||||
await this.handleStreamTool(command);
|
||||
}
|
||||
else {
|
||||
console.log("❌ Unknown command. Try 'list', 'call <tool_name>', 'stream <tool_name>', or 'quit'");
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
if (error instanceof Error && error.message === 'SIGINT') {
|
||||
console.log('\n\n👋 Goodbye!');
|
||||
break;
|
||||
}
|
||||
console.error('❌ Error:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
async listTools() {
|
||||
if (!this.client) {
|
||||
console.log('❌ Not connected to server');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const request = {
|
||||
method: 'tools/list',
|
||||
params: {}
|
||||
};
|
||||
const result = await this.client.request(request, ListToolsResultSchema);
|
||||
if (result.tools && result.tools.length > 0) {
|
||||
console.log('\n📋 Available tools:');
|
||||
result.tools.forEach((tool, index) => {
|
||||
console.log(`${index + 1}. ${tool.name}`);
|
||||
if (tool.description) {
|
||||
console.log(` Description: ${tool.description}`);
|
||||
}
|
||||
console.log();
|
||||
});
|
||||
}
|
||||
else {
|
||||
console.log('No tools available');
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.error('❌ Failed to list tools:', error);
|
||||
}
|
||||
}
|
||||
async handleCallTool(command) {
|
||||
const parts = command.split(/\s+/);
|
||||
const toolName = parts[1];
|
||||
if (!toolName) {
|
||||
console.log('❌ Please specify a tool name');
|
||||
return;
|
||||
}
|
||||
// Parse arguments (simple JSON-like format)
|
||||
let toolArgs = {};
|
||||
if (parts.length > 2) {
|
||||
const argsString = parts.slice(2).join(' ');
|
||||
try {
|
||||
toolArgs = JSON.parse(argsString);
|
||||
}
|
||||
catch {
|
||||
console.log('❌ Invalid arguments format (expected JSON)');
|
||||
return;
|
||||
}
|
||||
}
|
||||
await this.callTool(toolName, toolArgs);
|
||||
}
|
||||
async callTool(toolName, toolArgs) {
|
||||
if (!this.client) {
|
||||
console.log('❌ Not connected to server');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const request = {
|
||||
method: 'tools/call',
|
||||
params: {
|
||||
name: toolName,
|
||||
arguments: toolArgs
|
||||
}
|
||||
};
|
||||
const result = await this.client.request(request, CallToolResultSchema);
|
||||
console.log(`\n🔧 Tool '${toolName}' result:`);
|
||||
if (result.content) {
|
||||
result.content.forEach(content => {
|
||||
if (content.type === 'text') {
|
||||
console.log(content.text);
|
||||
}
|
||||
else {
|
||||
console.log(content);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
console.log(result);
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`❌ Failed to call tool '${toolName}':`, error);
|
||||
}
|
||||
}
|
||||
async handleStreamTool(command) {
|
||||
const parts = command.split(/\s+/);
|
||||
const toolName = parts[1];
|
||||
if (!toolName) {
|
||||
console.log('❌ Please specify a tool name');
|
||||
return;
|
||||
}
|
||||
// Parse arguments (simple JSON-like format)
|
||||
let toolArgs = {};
|
||||
if (parts.length > 2) {
|
||||
const argsString = parts.slice(2).join(' ');
|
||||
try {
|
||||
toolArgs = JSON.parse(argsString);
|
||||
}
|
||||
catch {
|
||||
console.log('❌ Invalid arguments format (expected JSON)');
|
||||
return;
|
||||
}
|
||||
}
|
||||
await this.streamTool(toolName, toolArgs);
|
||||
}
|
||||
async streamTool(toolName, toolArgs) {
|
||||
if (!this.client) {
|
||||
console.log('❌ Not connected to server');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// Using the experimental tasks API - WARNING: may change without notice
|
||||
console.log(`\n🔧 Streaming tool '${toolName}'...`);
|
||||
const stream = this.client.experimental.tasks.callToolStream({
|
||||
name: toolName,
|
||||
arguments: toolArgs
|
||||
}, CallToolResultSchema, {
|
||||
task: {
|
||||
taskId: `task-${Date.now()}`,
|
||||
ttl: 60000
|
||||
}
|
||||
});
|
||||
// Iterate through all messages yielded by the generator
|
||||
for await (const message of stream) {
|
||||
switch (message.type) {
|
||||
case 'taskCreated':
|
||||
console.log(`✓ Task created: ${message.task.taskId}`);
|
||||
break;
|
||||
case 'taskStatus':
|
||||
console.log(`⟳ Status: ${message.task.status}`);
|
||||
if (message.task.statusMessage) {
|
||||
console.log(` ${message.task.statusMessage}`);
|
||||
}
|
||||
break;
|
||||
case 'result':
|
||||
console.log('✓ Completed!');
|
||||
message.result.content.forEach(content => {
|
||||
if (content.type === 'text') {
|
||||
console.log(content.text);
|
||||
}
|
||||
else {
|
||||
console.log(content);
|
||||
}
|
||||
});
|
||||
break;
|
||||
case 'error':
|
||||
console.log('✗ Error:');
|
||||
console.log(` ${message.error.message}`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`❌ Failed to stream tool '${toolName}':`, error);
|
||||
}
|
||||
}
|
||||
close() {
|
||||
this.rl.close();
|
||||
if (this.client) {
|
||||
// Note: Client doesn't have a close method in the current implementation
|
||||
// This would typically close the transport connection
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Main entry point
|
||||
*/
|
||||
async function main() {
|
||||
const args = process.argv.slice(2);
|
||||
const serverUrl = args[0] || DEFAULT_SERVER_URL;
|
||||
const clientMetadataUrl = args[1];
|
||||
console.log('🚀 Simple MCP OAuth Client');
|
||||
console.log(`Connecting to: ${serverUrl}`);
|
||||
if (clientMetadataUrl) {
|
||||
console.log(`Client Metadata URL: ${clientMetadataUrl}`);
|
||||
}
|
||||
console.log();
|
||||
const client = new InteractiveOAuthClient(serverUrl, clientMetadataUrl);
|
||||
// Handle graceful shutdown
|
||||
process.on('SIGINT', () => {
|
||||
console.log('\n\n👋 Goodbye!');
|
||||
client.close();
|
||||
process.exit(0);
|
||||
});
|
||||
try {
|
||||
await client.connect();
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Failed to start client:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
finally {
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
// Run if this file is executed directly
|
||||
main().catch(error => {
|
||||
console.error('Unhandled error:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
//# sourceMappingURL=simpleOAuthClient.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleOAuthClient.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleOAuthClient.js.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
26
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleOAuthClientProvider.d.ts
generated
vendored
Normal file
26
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleOAuthClientProvider.d.ts
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
import { OAuthClientProvider } from '../../client/auth.js';
|
||||
import { OAuthClientInformationMixed, OAuthClientMetadata, OAuthTokens } from '../../shared/auth.js';
|
||||
/**
|
||||
* In-memory OAuth client provider for demonstration purposes
|
||||
* In production, you should persist tokens securely
|
||||
*/
|
||||
export declare class InMemoryOAuthClientProvider implements OAuthClientProvider {
|
||||
private readonly _redirectUrl;
|
||||
private readonly _clientMetadata;
|
||||
readonly clientMetadataUrl?: string | undefined;
|
||||
private _clientInformation?;
|
||||
private _tokens?;
|
||||
private _codeVerifier?;
|
||||
constructor(_redirectUrl: string | URL, _clientMetadata: OAuthClientMetadata, onRedirect?: (url: URL) => void, clientMetadataUrl?: string | undefined);
|
||||
private _onRedirect;
|
||||
get redirectUrl(): string | URL;
|
||||
get clientMetadata(): OAuthClientMetadata;
|
||||
clientInformation(): OAuthClientInformationMixed | undefined;
|
||||
saveClientInformation(clientInformation: OAuthClientInformationMixed): void;
|
||||
tokens(): OAuthTokens | undefined;
|
||||
saveTokens(tokens: OAuthTokens): void;
|
||||
redirectToAuthorization(authorizationUrl: URL): void;
|
||||
saveCodeVerifier(codeVerifier: string): void;
|
||||
codeVerifier(): string;
|
||||
}
|
||||
//# sourceMappingURL=simpleOAuthClientProvider.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleOAuthClientProvider.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleOAuthClientProvider.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"simpleOAuthClientProvider.d.ts","sourceRoot":"","sources":["../../../../src/examples/client/simpleOAuthClientProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,EAAE,2BAA2B,EAAE,mBAAmB,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAErG;;;GAGG;AACH,qBAAa,2BAA4B,YAAW,mBAAmB;IAM/D,OAAO,CAAC,QAAQ,CAAC,YAAY;IAC7B,OAAO,CAAC,QAAQ,CAAC,eAAe;aAEhB,iBAAiB,CAAC,EAAE,MAAM;IAR9C,OAAO,CAAC,kBAAkB,CAAC,CAA8B;IACzD,OAAO,CAAC,OAAO,CAAC,CAAc;IAC9B,OAAO,CAAC,aAAa,CAAC,CAAS;gBAGV,YAAY,EAAE,MAAM,GAAG,GAAG,EAC1B,eAAe,EAAE,mBAAmB,EACrD,UAAU,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,IAAI,EACf,iBAAiB,CAAC,EAAE,MAAM,YAAA;IAS9C,OAAO,CAAC,WAAW,CAAqB;IAExC,IAAI,WAAW,IAAI,MAAM,GAAG,GAAG,CAE9B;IAED,IAAI,cAAc,IAAI,mBAAmB,CAExC;IAED,iBAAiB,IAAI,2BAA2B,GAAG,SAAS;IAI5D,qBAAqB,CAAC,iBAAiB,EAAE,2BAA2B,GAAG,IAAI;IAI3E,MAAM,IAAI,WAAW,GAAG,SAAS;IAIjC,UAAU,CAAC,MAAM,EAAE,WAAW,GAAG,IAAI;IAIrC,uBAAuB,CAAC,gBAAgB,EAAE,GAAG,GAAG,IAAI;IAIpD,gBAAgB,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI;IAI5C,YAAY,IAAI,MAAM;CAMzB"}
|
||||
47
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleOAuthClientProvider.js
generated
vendored
Normal file
47
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleOAuthClientProvider.js
generated
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* In-memory OAuth client provider for demonstration purposes
|
||||
* In production, you should persist tokens securely
|
||||
*/
|
||||
export class InMemoryOAuthClientProvider {
|
||||
constructor(_redirectUrl, _clientMetadata, onRedirect, clientMetadataUrl) {
|
||||
this._redirectUrl = _redirectUrl;
|
||||
this._clientMetadata = _clientMetadata;
|
||||
this.clientMetadataUrl = clientMetadataUrl;
|
||||
this._onRedirect =
|
||||
onRedirect ||
|
||||
(url => {
|
||||
console.log(`Redirect to: ${url.toString()}`);
|
||||
});
|
||||
}
|
||||
get redirectUrl() {
|
||||
return this._redirectUrl;
|
||||
}
|
||||
get clientMetadata() {
|
||||
return this._clientMetadata;
|
||||
}
|
||||
clientInformation() {
|
||||
return this._clientInformation;
|
||||
}
|
||||
saveClientInformation(clientInformation) {
|
||||
this._clientInformation = clientInformation;
|
||||
}
|
||||
tokens() {
|
||||
return this._tokens;
|
||||
}
|
||||
saveTokens(tokens) {
|
||||
this._tokens = tokens;
|
||||
}
|
||||
redirectToAuthorization(authorizationUrl) {
|
||||
this._onRedirect(authorizationUrl);
|
||||
}
|
||||
saveCodeVerifier(codeVerifier) {
|
||||
this._codeVerifier = codeVerifier;
|
||||
}
|
||||
codeVerifier() {
|
||||
if (!this._codeVerifier) {
|
||||
throw new Error('No code verifier saved');
|
||||
}
|
||||
return this._codeVerifier;
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=simpleOAuthClientProvider.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleOAuthClientProvider.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleOAuthClientProvider.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"simpleOAuthClientProvider.js","sourceRoot":"","sources":["../../../../src/examples/client/simpleOAuthClientProvider.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,MAAM,OAAO,2BAA2B;IAKpC,YACqB,YAA0B,EAC1B,eAAoC,EACrD,UAA+B,EACf,iBAA0B;QAHzB,iBAAY,GAAZ,YAAY,CAAc;QAC1B,oBAAe,GAAf,eAAe,CAAqB;QAErC,sBAAiB,GAAjB,iBAAiB,CAAS;QAE1C,IAAI,CAAC,WAAW;YACZ,UAAU;gBACV,CAAC,GAAG,CAAC,EAAE;oBACH,OAAO,CAAC,GAAG,CAAC,gBAAgB,GAAG,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;gBAClD,CAAC,CAAC,CAAC;IACX,CAAC;IAID,IAAI,WAAW;QACX,OAAO,IAAI,CAAC,YAAY,CAAC;IAC7B,CAAC;IAED,IAAI,cAAc;QACd,OAAO,IAAI,CAAC,eAAe,CAAC;IAChC,CAAC;IAED,iBAAiB;QACb,OAAO,IAAI,CAAC,kBAAkB,CAAC;IACnC,CAAC;IAED,qBAAqB,CAAC,iBAA8C;QAChE,IAAI,CAAC,kBAAkB,GAAG,iBAAiB,CAAC;IAChD,CAAC;IAED,MAAM;QACF,OAAO,IAAI,CAAC,OAAO,CAAC;IACxB,CAAC;IAED,UAAU,CAAC,MAAmB;QAC1B,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IAC1B,CAAC;IAED,uBAAuB,CAAC,gBAAqB;QACzC,IAAI,CAAC,WAAW,CAAC,gBAAgB,CAAC,CAAC;IACvC,CAAC;IAED,gBAAgB,CAAC,YAAoB;QACjC,IAAI,CAAC,aAAa,GAAG,YAAY,CAAC;IACtC,CAAC;IAED,YAAY;QACR,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,CAAC;YACtB,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;QAC9C,CAAC;QACD,OAAO,IAAI,CAAC,aAAa,CAAC;IAC9B,CAAC;CACJ"}
|
||||
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleStreamableHttp.d.ts
generated
vendored
Normal file
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleStreamableHttp.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=simpleStreamableHttp.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleStreamableHttp.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleStreamableHttp.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"simpleStreamableHttp.d.ts","sourceRoot":"","sources":["../../../../src/examples/client/simpleStreamableHttp.ts"],"names":[],"mappings":""}
|
||||
855
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleStreamableHttp.js
generated
vendored
Normal file
855
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleStreamableHttp.js
generated
vendored
Normal file
@@ -0,0 +1,855 @@
|
||||
import { Client } from '../../client/index.js';
|
||||
import { StreamableHTTPClientTransport } from '../../client/streamableHttp.js';
|
||||
import { createInterface } from 'node:readline';
|
||||
import { ListToolsResultSchema, CallToolResultSchema, ListPromptsResultSchema, GetPromptResultSchema, ListResourcesResultSchema, LoggingMessageNotificationSchema, ResourceListChangedNotificationSchema, ElicitRequestSchema, ReadResourceResultSchema, RELATED_TASK_META_KEY, ErrorCode, McpError } from '../../types.js';
|
||||
import { InMemoryTaskStore } from '../../experimental/tasks/stores/in-memory.js';
|
||||
import { getDisplayName } from '../../shared/metadataUtils.js';
|
||||
import { Ajv } from 'ajv';
|
||||
// Create readline interface for user input
|
||||
const readline = createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
// Track received notifications for debugging resumability
|
||||
let notificationCount = 0;
|
||||
// Global client and transport for interactive commands
|
||||
let client = null;
|
||||
let transport = null;
|
||||
let serverUrl = 'http://localhost:3000/mcp';
|
||||
let notificationsToolLastEventId = undefined;
|
||||
let sessionId = undefined;
|
||||
async function main() {
|
||||
console.log('MCP Interactive Client');
|
||||
console.log('=====================');
|
||||
// Connect to server immediately with default settings
|
||||
await connect();
|
||||
// Print help and start the command loop
|
||||
printHelp();
|
||||
commandLoop();
|
||||
}
|
||||
function printHelp() {
|
||||
console.log('\nAvailable commands:');
|
||||
console.log(' connect [url] - Connect to MCP server (default: http://localhost:3000/mcp)');
|
||||
console.log(' disconnect - Disconnect from server');
|
||||
console.log(' terminate-session - Terminate the current session');
|
||||
console.log(' reconnect - Reconnect to the server');
|
||||
console.log(' list-tools - List available tools');
|
||||
console.log(' call-tool <name> [args] - Call a tool with optional JSON arguments');
|
||||
console.log(' call-tool-task <name> [args] - Call a tool with task-based execution (example: call-tool-task delay {"duration":3000})');
|
||||
console.log(' greet [name] - Call the greet tool');
|
||||
console.log(' multi-greet [name] - Call the multi-greet tool with notifications');
|
||||
console.log(' collect-info [type] - Test form elicitation with collect-user-info tool (contact/preferences/feedback)');
|
||||
console.log(' collect-info-task [type] - Test bidirectional task support (server+client tasks) with elicitation');
|
||||
console.log(' start-notifications [interval] [count] - Start periodic notifications');
|
||||
console.log(' run-notifications-tool-with-resumability [interval] [count] - Run notification tool with resumability');
|
||||
console.log(' list-prompts - List available prompts');
|
||||
console.log(' get-prompt [name] [args] - Get a prompt with optional JSON arguments');
|
||||
console.log(' list-resources - List available resources');
|
||||
console.log(' read-resource <uri> - Read a specific resource by URI');
|
||||
console.log(' help - Show this help');
|
||||
console.log(' quit - Exit the program');
|
||||
}
|
||||
function commandLoop() {
|
||||
readline.question('\n> ', async (input) => {
|
||||
const args = input.trim().split(/\s+/);
|
||||
const command = args[0]?.toLowerCase();
|
||||
try {
|
||||
switch (command) {
|
||||
case 'connect':
|
||||
await connect(args[1]);
|
||||
break;
|
||||
case 'disconnect':
|
||||
await disconnect();
|
||||
break;
|
||||
case 'terminate-session':
|
||||
await terminateSession();
|
||||
break;
|
||||
case 'reconnect':
|
||||
await reconnect();
|
||||
break;
|
||||
case 'list-tools':
|
||||
await listTools();
|
||||
break;
|
||||
case 'call-tool':
|
||||
if (args.length < 2) {
|
||||
console.log('Usage: call-tool <name> [args]');
|
||||
}
|
||||
else {
|
||||
const toolName = args[1];
|
||||
let toolArgs = {};
|
||||
if (args.length > 2) {
|
||||
try {
|
||||
toolArgs = JSON.parse(args.slice(2).join(' '));
|
||||
}
|
||||
catch {
|
||||
console.log('Invalid JSON arguments. Using empty args.');
|
||||
}
|
||||
}
|
||||
await callTool(toolName, toolArgs);
|
||||
}
|
||||
break;
|
||||
case 'greet':
|
||||
await callGreetTool(args[1] || 'MCP User');
|
||||
break;
|
||||
case 'multi-greet':
|
||||
await callMultiGreetTool(args[1] || 'MCP User');
|
||||
break;
|
||||
case 'collect-info':
|
||||
await callCollectInfoTool(args[1] || 'contact');
|
||||
break;
|
||||
case 'collect-info-task': {
|
||||
await callCollectInfoWithTask(args[1] || 'contact');
|
||||
break;
|
||||
}
|
||||
case 'start-notifications': {
|
||||
const interval = args[1] ? parseInt(args[1], 10) : 2000;
|
||||
const count = args[2] ? parseInt(args[2], 10) : 10;
|
||||
await startNotifications(interval, count);
|
||||
break;
|
||||
}
|
||||
case 'run-notifications-tool-with-resumability': {
|
||||
const interval = args[1] ? parseInt(args[1], 10) : 2000;
|
||||
const count = args[2] ? parseInt(args[2], 10) : 10;
|
||||
await runNotificationsToolWithResumability(interval, count);
|
||||
break;
|
||||
}
|
||||
case 'call-tool-task':
|
||||
if (args.length < 2) {
|
||||
console.log('Usage: call-tool-task <name> [args]');
|
||||
}
|
||||
else {
|
||||
const toolName = args[1];
|
||||
let toolArgs = {};
|
||||
if (args.length > 2) {
|
||||
try {
|
||||
toolArgs = JSON.parse(args.slice(2).join(' '));
|
||||
}
|
||||
catch {
|
||||
console.log('Invalid JSON arguments. Using empty args.');
|
||||
}
|
||||
}
|
||||
await callToolTask(toolName, toolArgs);
|
||||
}
|
||||
break;
|
||||
case 'list-prompts':
|
||||
await listPrompts();
|
||||
break;
|
||||
case 'get-prompt':
|
||||
if (args.length < 2) {
|
||||
console.log('Usage: get-prompt <name> [args]');
|
||||
}
|
||||
else {
|
||||
const promptName = args[1];
|
||||
let promptArgs = {};
|
||||
if (args.length > 2) {
|
||||
try {
|
||||
promptArgs = JSON.parse(args.slice(2).join(' '));
|
||||
}
|
||||
catch {
|
||||
console.log('Invalid JSON arguments. Using empty args.');
|
||||
}
|
||||
}
|
||||
await getPrompt(promptName, promptArgs);
|
||||
}
|
||||
break;
|
||||
case 'list-resources':
|
||||
await listResources();
|
||||
break;
|
||||
case 'read-resource':
|
||||
if (args.length < 2) {
|
||||
console.log('Usage: read-resource <uri>');
|
||||
}
|
||||
else {
|
||||
await readResource(args[1]);
|
||||
}
|
||||
break;
|
||||
case 'help':
|
||||
printHelp();
|
||||
break;
|
||||
case 'quit':
|
||||
case 'exit':
|
||||
await cleanup();
|
||||
return;
|
||||
default:
|
||||
if (command) {
|
||||
console.log(`Unknown command: ${command}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`Error executing command: ${error}`);
|
||||
}
|
||||
// Continue the command loop
|
||||
commandLoop();
|
||||
});
|
||||
}
|
||||
async function connect(url) {
|
||||
if (client) {
|
||||
console.log('Already connected. Disconnect first.');
|
||||
return;
|
||||
}
|
||||
if (url) {
|
||||
serverUrl = url;
|
||||
}
|
||||
console.log(`Connecting to ${serverUrl}...`);
|
||||
try {
|
||||
// Create task store for client-side task support
|
||||
const clientTaskStore = new InMemoryTaskStore();
|
||||
// Create a new client with form elicitation capability and task support
|
||||
client = new Client({
|
||||
name: 'example-client',
|
||||
version: '1.0.0'
|
||||
}, {
|
||||
capabilities: {
|
||||
elicitation: {
|
||||
form: {}
|
||||
},
|
||||
tasks: {
|
||||
requests: {
|
||||
elicitation: {
|
||||
create: {}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
taskStore: clientTaskStore
|
||||
});
|
||||
client.onerror = error => {
|
||||
console.error('\x1b[31mClient error:', error, '\x1b[0m');
|
||||
};
|
||||
// Set up elicitation request handler with proper validation and task support
|
||||
client.setRequestHandler(ElicitRequestSchema, async (request, extra) => {
|
||||
if (request.params.mode !== 'form') {
|
||||
throw new McpError(ErrorCode.InvalidParams, `Unsupported elicitation mode: ${request.params.mode}`);
|
||||
}
|
||||
console.log('\n🔔 Elicitation (form) Request Received:');
|
||||
console.log(`Message: ${request.params.message}`);
|
||||
console.log(`Related Task: ${request.params._meta?.[RELATED_TASK_META_KEY]?.taskId}`);
|
||||
console.log(`Task Creation Requested: ${request.params.task ? 'yes' : 'no'}`);
|
||||
console.log('Requested Schema:');
|
||||
console.log(JSON.stringify(request.params.requestedSchema, null, 2));
|
||||
// Helper to return result, optionally creating a task if requested
|
||||
const returnResult = async (result) => {
|
||||
if (request.params.task && extra.taskStore) {
|
||||
// Create a task and store the result
|
||||
const task = await extra.taskStore.createTask({ ttl: extra.taskRequestedTtl });
|
||||
await extra.taskStore.storeTaskResult(task.taskId, 'completed', result);
|
||||
console.log(`📋 Created client-side task: ${task.taskId}`);
|
||||
return { task };
|
||||
}
|
||||
return result;
|
||||
};
|
||||
const schema = request.params.requestedSchema;
|
||||
const properties = schema.properties;
|
||||
const required = schema.required || [];
|
||||
// Set up AJV validator for the requested schema
|
||||
const ajv = new Ajv();
|
||||
const validate = ajv.compile(schema);
|
||||
let attempts = 0;
|
||||
const maxAttempts = 3;
|
||||
while (attempts < maxAttempts) {
|
||||
attempts++;
|
||||
console.log(`\nPlease provide the following information (attempt ${attempts}/${maxAttempts}):`);
|
||||
const content = {};
|
||||
let inputCancelled = false;
|
||||
// Collect input for each field
|
||||
for (const [fieldName, fieldSchema] of Object.entries(properties)) {
|
||||
const field = fieldSchema;
|
||||
const isRequired = required.includes(fieldName);
|
||||
let prompt = `${field.title || fieldName}`;
|
||||
// Add helpful information to the prompt
|
||||
if (field.description) {
|
||||
prompt += ` (${field.description})`;
|
||||
}
|
||||
if (field.enum) {
|
||||
prompt += ` [options: ${field.enum.join(', ')}]`;
|
||||
}
|
||||
if (field.type === 'number' || field.type === 'integer') {
|
||||
if (field.minimum !== undefined && field.maximum !== undefined) {
|
||||
prompt += ` [${field.minimum}-${field.maximum}]`;
|
||||
}
|
||||
else if (field.minimum !== undefined) {
|
||||
prompt += ` [min: ${field.minimum}]`;
|
||||
}
|
||||
else if (field.maximum !== undefined) {
|
||||
prompt += ` [max: ${field.maximum}]`;
|
||||
}
|
||||
}
|
||||
if (field.type === 'string' && field.format) {
|
||||
prompt += ` [format: ${field.format}]`;
|
||||
}
|
||||
if (isRequired) {
|
||||
prompt += ' *required*';
|
||||
}
|
||||
if (field.default !== undefined) {
|
||||
prompt += ` [default: ${field.default}]`;
|
||||
}
|
||||
prompt += ': ';
|
||||
const answer = await new Promise(resolve => {
|
||||
readline.question(prompt, input => {
|
||||
resolve(input.trim());
|
||||
});
|
||||
});
|
||||
// Check for cancellation
|
||||
if (answer.toLowerCase() === 'cancel' || answer.toLowerCase() === 'c') {
|
||||
inputCancelled = true;
|
||||
break;
|
||||
}
|
||||
// Parse and validate the input
|
||||
try {
|
||||
if (answer === '' && field.default !== undefined) {
|
||||
content[fieldName] = field.default;
|
||||
}
|
||||
else if (answer === '' && !isRequired) {
|
||||
// Skip optional empty fields
|
||||
continue;
|
||||
}
|
||||
else if (answer === '') {
|
||||
throw new Error(`${fieldName} is required`);
|
||||
}
|
||||
else {
|
||||
// Parse the value based on type
|
||||
let parsedValue;
|
||||
if (field.type === 'boolean') {
|
||||
parsedValue = answer.toLowerCase() === 'true' || answer.toLowerCase() === 'yes' || answer === '1';
|
||||
}
|
||||
else if (field.type === 'number') {
|
||||
parsedValue = parseFloat(answer);
|
||||
if (isNaN(parsedValue)) {
|
||||
throw new Error(`${fieldName} must be a valid number`);
|
||||
}
|
||||
}
|
||||
else if (field.type === 'integer') {
|
||||
parsedValue = parseInt(answer, 10);
|
||||
if (isNaN(parsedValue)) {
|
||||
throw new Error(`${fieldName} must be a valid integer`);
|
||||
}
|
||||
}
|
||||
else if (field.enum) {
|
||||
if (!field.enum.includes(answer)) {
|
||||
throw new Error(`${fieldName} must be one of: ${field.enum.join(', ')}`);
|
||||
}
|
||||
parsedValue = answer;
|
||||
}
|
||||
else {
|
||||
parsedValue = answer;
|
||||
}
|
||||
content[fieldName] = parsedValue;
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.log(`❌ Error: ${error}`);
|
||||
// Continue to next attempt
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (inputCancelled) {
|
||||
return returnResult({ action: 'cancel' });
|
||||
}
|
||||
// If we didn't complete all fields due to an error, try again
|
||||
if (Object.keys(content).length !==
|
||||
Object.keys(properties).filter(name => required.includes(name) || content[name] !== undefined).length) {
|
||||
if (attempts < maxAttempts) {
|
||||
console.log('Please try again...');
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
console.log('Maximum attempts reached. Declining request.');
|
||||
return returnResult({ action: 'decline' });
|
||||
}
|
||||
}
|
||||
// Validate the complete object against the schema
|
||||
const isValid = validate(content);
|
||||
if (!isValid) {
|
||||
console.log('❌ Validation errors:');
|
||||
validate.errors?.forEach(error => {
|
||||
console.log(` - ${error.instancePath || 'root'}: ${error.message}`);
|
||||
});
|
||||
if (attempts < maxAttempts) {
|
||||
console.log('Please correct the errors and try again...');
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
console.log('Maximum attempts reached. Declining request.');
|
||||
return returnResult({ action: 'decline' });
|
||||
}
|
||||
}
|
||||
// Show the collected data and ask for confirmation
|
||||
console.log('\n✅ Collected data:');
|
||||
console.log(JSON.stringify(content, null, 2));
|
||||
const confirmAnswer = await new Promise(resolve => {
|
||||
readline.question('\nSubmit this information? (yes/no/cancel): ', input => {
|
||||
resolve(input.trim().toLowerCase());
|
||||
});
|
||||
});
|
||||
switch (confirmAnswer) {
|
||||
case 'yes':
|
||||
case 'y': {
|
||||
return returnResult({
|
||||
action: 'accept',
|
||||
content: content
|
||||
});
|
||||
}
|
||||
case 'cancel':
|
||||
case 'c': {
|
||||
return returnResult({ action: 'cancel' });
|
||||
}
|
||||
case 'no':
|
||||
case 'n': {
|
||||
if (attempts < maxAttempts) {
|
||||
console.log('Please re-enter the information...');
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
return returnResult({ action: 'decline' });
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log('Maximum attempts reached. Declining request.');
|
||||
return returnResult({ action: 'decline' });
|
||||
});
|
||||
transport = new StreamableHTTPClientTransport(new URL(serverUrl), {
|
||||
sessionId: sessionId
|
||||
});
|
||||
// Set up notification handlers
|
||||
client.setNotificationHandler(LoggingMessageNotificationSchema, notification => {
|
||||
notificationCount++;
|
||||
console.log(`\nNotification #${notificationCount}: ${notification.params.level} - ${notification.params.data}`);
|
||||
// Re-display the prompt
|
||||
process.stdout.write('> ');
|
||||
});
|
||||
client.setNotificationHandler(ResourceListChangedNotificationSchema, async (_) => {
|
||||
console.log(`\nResource list changed notification received!`);
|
||||
try {
|
||||
if (!client) {
|
||||
console.log('Client disconnected, cannot fetch resources');
|
||||
return;
|
||||
}
|
||||
const resourcesResult = await client.request({
|
||||
method: 'resources/list',
|
||||
params: {}
|
||||
}, ListResourcesResultSchema);
|
||||
console.log('Available resources count:', resourcesResult.resources.length);
|
||||
}
|
||||
catch {
|
||||
console.log('Failed to list resources after change notification');
|
||||
}
|
||||
// Re-display the prompt
|
||||
process.stdout.write('> ');
|
||||
});
|
||||
// Connect the client
|
||||
await client.connect(transport);
|
||||
sessionId = transport.sessionId;
|
||||
console.log('Transport created with session ID:', sessionId);
|
||||
console.log('Connected to MCP server');
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Failed to connect:', error);
|
||||
client = null;
|
||||
transport = null;
|
||||
}
|
||||
}
|
||||
async function disconnect() {
|
||||
if (!client || !transport) {
|
||||
console.log('Not connected.');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await transport.close();
|
||||
console.log('Disconnected from MCP server');
|
||||
client = null;
|
||||
transport = null;
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error disconnecting:', error);
|
||||
}
|
||||
}
|
||||
async function terminateSession() {
|
||||
if (!client || !transport) {
|
||||
console.log('Not connected.');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
console.log('Terminating session with ID:', transport.sessionId);
|
||||
await transport.terminateSession();
|
||||
console.log('Session terminated successfully');
|
||||
// Check if sessionId was cleared after termination
|
||||
if (!transport.sessionId) {
|
||||
console.log('Session ID has been cleared');
|
||||
sessionId = undefined;
|
||||
// Also close the transport and clear client objects
|
||||
await transport.close();
|
||||
console.log('Transport closed after session termination');
|
||||
client = null;
|
||||
transport = null;
|
||||
}
|
||||
else {
|
||||
console.log('Server responded with 405 Method Not Allowed (session termination not supported)');
|
||||
console.log('Session ID is still active:', transport.sessionId);
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error terminating session:', error);
|
||||
}
|
||||
}
|
||||
async function reconnect() {
|
||||
if (client) {
|
||||
await disconnect();
|
||||
}
|
||||
await connect();
|
||||
}
|
||||
async function listTools() {
|
||||
if (!client) {
|
||||
console.log('Not connected to server.');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const toolsRequest = {
|
||||
method: 'tools/list',
|
||||
params: {}
|
||||
};
|
||||
const toolsResult = await client.request(toolsRequest, ListToolsResultSchema);
|
||||
console.log('Available tools:');
|
||||
if (toolsResult.tools.length === 0) {
|
||||
console.log(' No tools available');
|
||||
}
|
||||
else {
|
||||
for (const tool of toolsResult.tools) {
|
||||
console.log(` - id: ${tool.name}, name: ${getDisplayName(tool)}, description: ${tool.description}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.log(`Tools not supported by this server (${error})`);
|
||||
}
|
||||
}
|
||||
async function callTool(name, args) {
|
||||
if (!client) {
|
||||
console.log('Not connected to server.');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const request = {
|
||||
method: 'tools/call',
|
||||
params: {
|
||||
name,
|
||||
arguments: args
|
||||
}
|
||||
};
|
||||
console.log(`Calling tool '${name}' with args:`, args);
|
||||
const result = await client.request(request, CallToolResultSchema);
|
||||
console.log('Tool result:');
|
||||
const resourceLinks = [];
|
||||
result.content.forEach(item => {
|
||||
if (item.type === 'text') {
|
||||
console.log(` ${item.text}`);
|
||||
}
|
||||
else if (item.type === 'resource_link') {
|
||||
const resourceLink = item;
|
||||
resourceLinks.push(resourceLink);
|
||||
console.log(` 📁 Resource Link: ${resourceLink.name}`);
|
||||
console.log(` URI: ${resourceLink.uri}`);
|
||||
if (resourceLink.mimeType) {
|
||||
console.log(` Type: ${resourceLink.mimeType}`);
|
||||
}
|
||||
if (resourceLink.description) {
|
||||
console.log(` Description: ${resourceLink.description}`);
|
||||
}
|
||||
}
|
||||
else if (item.type === 'resource') {
|
||||
console.log(` [Embedded Resource: ${item.resource.uri}]`);
|
||||
}
|
||||
else if (item.type === 'image') {
|
||||
console.log(` [Image: ${item.mimeType}]`);
|
||||
}
|
||||
else if (item.type === 'audio') {
|
||||
console.log(` [Audio: ${item.mimeType}]`);
|
||||
}
|
||||
else {
|
||||
console.log(` [Unknown content type]:`, item);
|
||||
}
|
||||
});
|
||||
// Offer to read resource links
|
||||
if (resourceLinks.length > 0) {
|
||||
console.log(`\nFound ${resourceLinks.length} resource link(s). Use 'read-resource <uri>' to read their content.`);
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.log(`Error calling tool ${name}: ${error}`);
|
||||
}
|
||||
}
|
||||
async function callGreetTool(name) {
|
||||
await callTool('greet', { name });
|
||||
}
|
||||
async function callMultiGreetTool(name) {
|
||||
console.log('Calling multi-greet tool with notifications...');
|
||||
await callTool('multi-greet', { name });
|
||||
}
|
||||
async function callCollectInfoTool(infoType) {
|
||||
console.log(`Testing form elicitation with collect-user-info tool (${infoType})...`);
|
||||
await callTool('collect-user-info', { infoType });
|
||||
}
|
||||
async function callCollectInfoWithTask(infoType) {
|
||||
console.log(`\n🔄 Testing bidirectional task support with collect-user-info-task tool (${infoType})...`);
|
||||
console.log('This will create a task on the server, which will elicit input and create a task on the client.\n');
|
||||
await callToolTask('collect-user-info-task', { infoType });
|
||||
}
|
||||
async function startNotifications(interval, count) {
|
||||
console.log(`Starting notification stream: interval=${interval}ms, count=${count || 'unlimited'}`);
|
||||
await callTool('start-notification-stream', { interval, count });
|
||||
}
|
||||
async function runNotificationsToolWithResumability(interval, count) {
|
||||
if (!client) {
|
||||
console.log('Not connected to server.');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
console.log(`Starting notification stream with resumability: interval=${interval}ms, count=${count || 'unlimited'}`);
|
||||
console.log(`Using resumption token: ${notificationsToolLastEventId || 'none'}`);
|
||||
const request = {
|
||||
method: 'tools/call',
|
||||
params: {
|
||||
name: 'start-notification-stream',
|
||||
arguments: { interval, count }
|
||||
}
|
||||
};
|
||||
const onLastEventIdUpdate = (event) => {
|
||||
notificationsToolLastEventId = event;
|
||||
console.log(`Updated resumption token: ${event}`);
|
||||
};
|
||||
const result = await client.request(request, CallToolResultSchema, {
|
||||
resumptionToken: notificationsToolLastEventId,
|
||||
onresumptiontoken: onLastEventIdUpdate
|
||||
});
|
||||
console.log('Tool result:');
|
||||
result.content.forEach(item => {
|
||||
if (item.type === 'text') {
|
||||
console.log(` ${item.text}`);
|
||||
}
|
||||
else {
|
||||
console.log(` ${item.type} content:`, item);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
console.log(`Error starting notification stream: ${error}`);
|
||||
}
|
||||
}
|
||||
async function listPrompts() {
|
||||
if (!client) {
|
||||
console.log('Not connected to server.');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const promptsRequest = {
|
||||
method: 'prompts/list',
|
||||
params: {}
|
||||
};
|
||||
const promptsResult = await client.request(promptsRequest, ListPromptsResultSchema);
|
||||
console.log('Available prompts:');
|
||||
if (promptsResult.prompts.length === 0) {
|
||||
console.log(' No prompts available');
|
||||
}
|
||||
else {
|
||||
for (const prompt of promptsResult.prompts) {
|
||||
console.log(` - id: ${prompt.name}, name: ${getDisplayName(prompt)}, description: ${prompt.description}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.log(`Prompts not supported by this server (${error})`);
|
||||
}
|
||||
}
|
||||
async function getPrompt(name, args) {
|
||||
if (!client) {
|
||||
console.log('Not connected to server.');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const promptRequest = {
|
||||
method: 'prompts/get',
|
||||
params: {
|
||||
name,
|
||||
arguments: args
|
||||
}
|
||||
};
|
||||
const promptResult = await client.request(promptRequest, GetPromptResultSchema);
|
||||
console.log('Prompt template:');
|
||||
promptResult.messages.forEach((msg, index) => {
|
||||
console.log(` [${index + 1}] ${msg.role}: ${msg.content.type === 'text' ? msg.content.text : JSON.stringify(msg.content)}`);
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
console.log(`Error getting prompt ${name}: ${error}`);
|
||||
}
|
||||
}
|
||||
async function listResources() {
|
||||
if (!client) {
|
||||
console.log('Not connected to server.');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const resourcesRequest = {
|
||||
method: 'resources/list',
|
||||
params: {}
|
||||
};
|
||||
const resourcesResult = await client.request(resourcesRequest, ListResourcesResultSchema);
|
||||
console.log('Available resources:');
|
||||
if (resourcesResult.resources.length === 0) {
|
||||
console.log(' No resources available');
|
||||
}
|
||||
else {
|
||||
for (const resource of resourcesResult.resources) {
|
||||
console.log(` - id: ${resource.name}, name: ${getDisplayName(resource)}, description: ${resource.uri}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.log(`Resources not supported by this server (${error})`);
|
||||
}
|
||||
}
|
||||
async function readResource(uri) {
|
||||
if (!client) {
|
||||
console.log('Not connected to server.');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const request = {
|
||||
method: 'resources/read',
|
||||
params: { uri }
|
||||
};
|
||||
console.log(`Reading resource: ${uri}`);
|
||||
const result = await client.request(request, ReadResourceResultSchema);
|
||||
console.log('Resource contents:');
|
||||
for (const content of result.contents) {
|
||||
console.log(` URI: ${content.uri}`);
|
||||
if (content.mimeType) {
|
||||
console.log(` Type: ${content.mimeType}`);
|
||||
}
|
||||
if ('text' in content && typeof content.text === 'string') {
|
||||
console.log(' Content:');
|
||||
console.log(' ---');
|
||||
console.log(content.text
|
||||
.split('\n')
|
||||
.map((line) => ' ' + line)
|
||||
.join('\n'));
|
||||
console.log(' ---');
|
||||
}
|
||||
else if ('blob' in content && typeof content.blob === 'string') {
|
||||
console.log(` [Binary data: ${content.blob.length} bytes]`);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.log(`Error reading resource ${uri}: ${error}`);
|
||||
}
|
||||
}
|
||||
async function callToolTask(name, args) {
|
||||
if (!client) {
|
||||
console.log('Not connected to server.');
|
||||
return;
|
||||
}
|
||||
console.log(`Calling tool '${name}' with task-based execution...`);
|
||||
console.log('Arguments:', args);
|
||||
// Use task-based execution - call now, fetch later
|
||||
// Using the experimental tasks API - WARNING: may change without notice
|
||||
console.log('This will return immediately while processing continues in the background...');
|
||||
try {
|
||||
// Call the tool with task metadata using streaming API
|
||||
const stream = client.experimental.tasks.callToolStream({
|
||||
name,
|
||||
arguments: args
|
||||
}, CallToolResultSchema, {
|
||||
task: {
|
||||
ttl: 60000 // Keep results for 60 seconds
|
||||
}
|
||||
});
|
||||
console.log('Waiting for task completion...');
|
||||
let lastStatus = '';
|
||||
for await (const message of stream) {
|
||||
switch (message.type) {
|
||||
case 'taskCreated':
|
||||
console.log('Task created successfully with ID:', message.task.taskId);
|
||||
break;
|
||||
case 'taskStatus':
|
||||
if (lastStatus !== message.task.status) {
|
||||
console.log(` ${message.task.status}${message.task.statusMessage ? ` - ${message.task.statusMessage}` : ''}`);
|
||||
}
|
||||
lastStatus = message.task.status;
|
||||
break;
|
||||
case 'result':
|
||||
console.log('Task completed!');
|
||||
console.log('Tool result:');
|
||||
message.result.content.forEach(item => {
|
||||
if (item.type === 'text') {
|
||||
console.log(` ${item.text}`);
|
||||
}
|
||||
});
|
||||
break;
|
||||
case 'error':
|
||||
throw message.error;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.log(`Error with task-based execution: ${error}`);
|
||||
}
|
||||
}
|
||||
async function cleanup() {
|
||||
if (client && transport) {
|
||||
try {
|
||||
// First try to terminate the session gracefully
|
||||
if (transport.sessionId) {
|
||||
try {
|
||||
console.log('Terminating session before exit...');
|
||||
await transport.terminateSession();
|
||||
console.log('Session terminated successfully');
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error terminating session:', error);
|
||||
}
|
||||
}
|
||||
// Then close the transport
|
||||
await transport.close();
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error closing transport:', error);
|
||||
}
|
||||
}
|
||||
process.stdin.setRawMode(false);
|
||||
readline.close();
|
||||
console.log('\nGoodbye!');
|
||||
process.exit(0);
|
||||
}
|
||||
// Set up raw mode for keyboard input to capture Escape key
|
||||
process.stdin.setRawMode(true);
|
||||
process.stdin.on('data', async (data) => {
|
||||
// Check for Escape key (27)
|
||||
if (data.length === 1 && data[0] === 27) {
|
||||
console.log('\nESC key pressed. Disconnecting from server...');
|
||||
// Abort current operation and disconnect from server
|
||||
if (client && transport) {
|
||||
await disconnect();
|
||||
console.log('Disconnected. Press Enter to continue.');
|
||||
}
|
||||
else {
|
||||
console.log('Not connected to server.');
|
||||
}
|
||||
// Re-display the prompt
|
||||
process.stdout.write('> ');
|
||||
}
|
||||
});
|
||||
// Handle Ctrl+C
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('\nReceived SIGINT. Cleaning up...');
|
||||
await cleanup();
|
||||
});
|
||||
// Start the interactive client
|
||||
main().catch((error) => {
|
||||
console.error('Error running MCP client:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
//# sourceMappingURL=simpleStreamableHttp.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleStreamableHttp.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleStreamableHttp.js.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
10
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleTaskInteractiveClient.d.ts
generated
vendored
Normal file
10
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleTaskInteractiveClient.d.ts
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* Simple interactive task client demonstrating elicitation and sampling responses.
|
||||
*
|
||||
* This client connects to simpleTaskInteractive.ts server and demonstrates:
|
||||
* - Handling elicitation requests (y/n confirmation)
|
||||
* - Handling sampling requests (returns a hardcoded haiku)
|
||||
* - Using task-based tool execution with streaming
|
||||
*/
|
||||
export {};
|
||||
//# sourceMappingURL=simpleTaskInteractiveClient.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleTaskInteractiveClient.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleTaskInteractiveClient.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"simpleTaskInteractiveClient.d.ts","sourceRoot":"","sources":["../../../../src/examples/client/simpleTaskInteractiveClient.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG"}
|
||||
155
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleTaskInteractiveClient.js
generated
vendored
Normal file
155
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleTaskInteractiveClient.js
generated
vendored
Normal file
@@ -0,0 +1,155 @@
|
||||
/**
|
||||
* Simple interactive task client demonstrating elicitation and sampling responses.
|
||||
*
|
||||
* This client connects to simpleTaskInteractive.ts server and demonstrates:
|
||||
* - Handling elicitation requests (y/n confirmation)
|
||||
* - Handling sampling requests (returns a hardcoded haiku)
|
||||
* - Using task-based tool execution with streaming
|
||||
*/
|
||||
import { Client } from '../../client/index.js';
|
||||
import { StreamableHTTPClientTransport } from '../../client/streamableHttp.js';
|
||||
import { createInterface } from 'node:readline';
|
||||
import { CallToolResultSchema, ElicitRequestSchema, CreateMessageRequestSchema, ErrorCode, McpError } from '../../types.js';
|
||||
// Create readline interface for user input
|
||||
const readline = createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
function question(prompt) {
|
||||
return new Promise(resolve => {
|
||||
readline.question(prompt, answer => {
|
||||
resolve(answer.trim());
|
||||
});
|
||||
});
|
||||
}
|
||||
function getTextContent(result) {
|
||||
const textContent = result.content.find((c) => c.type === 'text');
|
||||
return textContent?.text ?? '(no text)';
|
||||
}
|
||||
async function elicitationCallback(params) {
|
||||
console.log(`\n[Elicitation] Server asks: ${params.message}`);
|
||||
// Simple terminal prompt for y/n
|
||||
const response = await question('Your response (y/n): ');
|
||||
const confirmed = ['y', 'yes', 'true', '1'].includes(response.toLowerCase());
|
||||
console.log(`[Elicitation] Responding with: confirm=${confirmed}`);
|
||||
return { action: 'accept', content: { confirm: confirmed } };
|
||||
}
|
||||
async function samplingCallback(params) {
|
||||
// Get the prompt from the first message
|
||||
let prompt = 'unknown';
|
||||
if (params.messages && params.messages.length > 0) {
|
||||
const firstMessage = params.messages[0];
|
||||
const content = firstMessage.content;
|
||||
if (typeof content === 'object' && !Array.isArray(content) && content.type === 'text' && 'text' in content) {
|
||||
prompt = content.text;
|
||||
}
|
||||
else if (Array.isArray(content)) {
|
||||
const textPart = content.find(c => c.type === 'text' && 'text' in c);
|
||||
if (textPart && 'text' in textPart) {
|
||||
prompt = textPart.text;
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log(`\n[Sampling] Server requests LLM completion for: ${prompt}`);
|
||||
// Return a hardcoded haiku (in real use, call your LLM here)
|
||||
const haiku = `Cherry blossoms fall
|
||||
Softly on the quiet pond
|
||||
Spring whispers goodbye`;
|
||||
console.log('[Sampling] Responding with haiku');
|
||||
return {
|
||||
model: 'mock-haiku-model',
|
||||
role: 'assistant',
|
||||
content: { type: 'text', text: haiku }
|
||||
};
|
||||
}
|
||||
async function run(url) {
|
||||
console.log('Simple Task Interactive Client');
|
||||
console.log('==============================');
|
||||
console.log(`Connecting to ${url}...`);
|
||||
// Create client with elicitation and sampling capabilities
|
||||
const client = new Client({ name: 'simple-task-interactive-client', version: '1.0.0' }, {
|
||||
capabilities: {
|
||||
elicitation: { form: {} },
|
||||
sampling: {}
|
||||
}
|
||||
});
|
||||
// Set up elicitation request handler
|
||||
client.setRequestHandler(ElicitRequestSchema, async (request) => {
|
||||
if (request.params.mode && request.params.mode !== 'form') {
|
||||
throw new McpError(ErrorCode.InvalidParams, `Unsupported elicitation mode: ${request.params.mode}`);
|
||||
}
|
||||
return elicitationCallback(request.params);
|
||||
});
|
||||
// Set up sampling request handler
|
||||
client.setRequestHandler(CreateMessageRequestSchema, async (request) => {
|
||||
return samplingCallback(request.params);
|
||||
});
|
||||
// Connect to server
|
||||
const transport = new StreamableHTTPClientTransport(new URL(url));
|
||||
await client.connect(transport);
|
||||
console.log('Connected!\n');
|
||||
// List tools
|
||||
const toolsResult = await client.listTools();
|
||||
console.log(`Available tools: ${toolsResult.tools.map(t => t.name).join(', ')}`);
|
||||
// Demo 1: Elicitation (confirm_delete)
|
||||
console.log('\n--- Demo 1: Elicitation ---');
|
||||
console.log('Calling confirm_delete tool...');
|
||||
const confirmStream = client.experimental.tasks.callToolStream({ name: 'confirm_delete', arguments: { filename: 'important.txt' } }, CallToolResultSchema, { task: { ttl: 60000 } });
|
||||
for await (const message of confirmStream) {
|
||||
switch (message.type) {
|
||||
case 'taskCreated':
|
||||
console.log(`Task created: ${message.task.taskId}`);
|
||||
break;
|
||||
case 'taskStatus':
|
||||
console.log(`Task status: ${message.task.status}`);
|
||||
break;
|
||||
case 'result':
|
||||
console.log(`Result: ${getTextContent(message.result)}`);
|
||||
break;
|
||||
case 'error':
|
||||
console.error(`Error: ${message.error}`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Demo 2: Sampling (write_haiku)
|
||||
console.log('\n--- Demo 2: Sampling ---');
|
||||
console.log('Calling write_haiku tool...');
|
||||
const haikuStream = client.experimental.tasks.callToolStream({ name: 'write_haiku', arguments: { topic: 'autumn leaves' } }, CallToolResultSchema, {
|
||||
task: { ttl: 60000 }
|
||||
});
|
||||
for await (const message of haikuStream) {
|
||||
switch (message.type) {
|
||||
case 'taskCreated':
|
||||
console.log(`Task created: ${message.task.taskId}`);
|
||||
break;
|
||||
case 'taskStatus':
|
||||
console.log(`Task status: ${message.task.status}`);
|
||||
break;
|
||||
case 'result':
|
||||
console.log(`Result:\n${getTextContent(message.result)}`);
|
||||
break;
|
||||
case 'error':
|
||||
console.error(`Error: ${message.error}`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Cleanup
|
||||
console.log('\nDemo complete. Closing connection...');
|
||||
await transport.close();
|
||||
readline.close();
|
||||
}
|
||||
// Parse command line arguments
|
||||
const args = process.argv.slice(2);
|
||||
let url = 'http://localhost:8000/mcp';
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
if (args[i] === '--url' && args[i + 1]) {
|
||||
url = args[i + 1];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
// Run the client
|
||||
run(url).catch(error => {
|
||||
console.error('Error running client:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
//# sourceMappingURL=simpleTaskInteractiveClient.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleTaskInteractiveClient.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/simpleTaskInteractiveClient.js.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/ssePollingClient.d.ts
generated
vendored
Normal file
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/ssePollingClient.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=ssePollingClient.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/ssePollingClient.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/ssePollingClient.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"ssePollingClient.d.ts","sourceRoot":"","sources":["../../../../src/examples/client/ssePollingClient.ts"],"names":[],"mappings":""}
|
||||
93
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/ssePollingClient.js
generated
vendored
Normal file
93
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/ssePollingClient.js
generated
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* SSE Polling Example Client (SEP-1699)
|
||||
*
|
||||
* This example demonstrates client-side behavior during server-initiated
|
||||
* SSE stream disconnection and automatic reconnection.
|
||||
*
|
||||
* Key features demonstrated:
|
||||
* - Automatic reconnection when server closes SSE stream
|
||||
* - Event replay via Last-Event-ID header
|
||||
* - Resumption token tracking via onresumptiontoken callback
|
||||
*
|
||||
* Run with: npx tsx src/examples/client/ssePollingClient.ts
|
||||
* Requires: ssePollingExample.ts server running on port 3001
|
||||
*/
|
||||
import { Client } from '../../client/index.js';
|
||||
import { StreamableHTTPClientTransport } from '../../client/streamableHttp.js';
|
||||
import { CallToolResultSchema, LoggingMessageNotificationSchema } from '../../types.js';
|
||||
const SERVER_URL = 'http://localhost:3001/mcp';
|
||||
async function main() {
|
||||
console.log('SSE Polling Example Client');
|
||||
console.log('==========================');
|
||||
console.log(`Connecting to ${SERVER_URL}...`);
|
||||
console.log('');
|
||||
// Create transport with reconnection options
|
||||
const transport = new StreamableHTTPClientTransport(new URL(SERVER_URL), {
|
||||
// Use default reconnection options - SDK handles automatic reconnection
|
||||
});
|
||||
// Track the last event ID for debugging
|
||||
let lastEventId;
|
||||
// Set up transport error handler to observe disconnections
|
||||
// Filter out expected errors from SSE reconnection
|
||||
transport.onerror = error => {
|
||||
// Skip abort errors during intentional close
|
||||
if (error.message.includes('AbortError'))
|
||||
return;
|
||||
// Show SSE disconnect (expected when server closes stream)
|
||||
if (error.message.includes('Unexpected end of JSON')) {
|
||||
console.log('[Transport] SSE stream disconnected - client will auto-reconnect');
|
||||
return;
|
||||
}
|
||||
console.log(`[Transport] Error: ${error.message}`);
|
||||
};
|
||||
// Set up transport close handler
|
||||
transport.onclose = () => {
|
||||
console.log('[Transport] Connection closed');
|
||||
};
|
||||
// Create and connect client
|
||||
const client = new Client({
|
||||
name: 'sse-polling-client',
|
||||
version: '1.0.0'
|
||||
});
|
||||
// Set up notification handler to receive progress updates
|
||||
client.setNotificationHandler(LoggingMessageNotificationSchema, notification => {
|
||||
const data = notification.params.data;
|
||||
console.log(`[Notification] ${data}`);
|
||||
});
|
||||
try {
|
||||
await client.connect(transport);
|
||||
console.log('[Client] Connected successfully');
|
||||
console.log('');
|
||||
// Call the long-task tool
|
||||
console.log('[Client] Calling long-task tool...');
|
||||
console.log('[Client] Server will disconnect mid-task to demonstrate polling');
|
||||
console.log('');
|
||||
const result = await client.request({
|
||||
method: 'tools/call',
|
||||
params: {
|
||||
name: 'long-task',
|
||||
arguments: {}
|
||||
}
|
||||
}, CallToolResultSchema, {
|
||||
// Track resumption tokens for debugging
|
||||
onresumptiontoken: token => {
|
||||
lastEventId = token;
|
||||
console.log(`[Event ID] ${token}`);
|
||||
}
|
||||
});
|
||||
console.log('');
|
||||
console.log('[Client] Tool completed!');
|
||||
console.log(`[Result] ${JSON.stringify(result.content, null, 2)}`);
|
||||
console.log('');
|
||||
console.log(`[Debug] Final event ID: ${lastEventId}`);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('[Error]', error);
|
||||
}
|
||||
finally {
|
||||
await transport.close();
|
||||
console.log('[Client] Disconnected');
|
||||
}
|
||||
}
|
||||
main().catch(console.error);
|
||||
//# sourceMappingURL=ssePollingClient.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/ssePollingClient.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/ssePollingClient.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"ssePollingClient.js","sourceRoot":"","sources":["../../../../src/examples/client/ssePollingClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,6BAA6B,EAAE,MAAM,gCAAgC,CAAC;AAC/E,OAAO,EAAE,oBAAoB,EAAE,gCAAgC,EAAE,MAAM,gBAAgB,CAAC;AAExF,MAAM,UAAU,GAAG,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pC,CAAC;YAAS,CAAC;QACP,MAAM,SAAS,CAAC,KAAK,EAAE,CAAC;QACxB,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;IACzC,CAAC;AACL,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC"}
|
||||
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/streamableHttpWithSseFallbackClient.d.ts
generated
vendored
Normal file
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/streamableHttpWithSseFallbackClient.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=streamableHttpWithSseFallbackClient.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/streamableHttpWithSseFallbackClient.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/streamableHttpWithSseFallbackClient.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"streamableHttpWithSseFallbackClient.d.ts","sourceRoot":"","sources":["../../../../src/examples/client/streamableHttpWithSseFallbackClient.ts"],"names":[],"mappings":""}
|
||||
166
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/streamableHttpWithSseFallbackClient.js
generated
vendored
Normal file
166
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/streamableHttpWithSseFallbackClient.js
generated
vendored
Normal file
@@ -0,0 +1,166 @@
|
||||
import { Client } from '../../client/index.js';
|
||||
import { StreamableHTTPClientTransport } from '../../client/streamableHttp.js';
|
||||
import { SSEClientTransport } from '../../client/sse.js';
|
||||
import { ListToolsResultSchema, CallToolResultSchema, LoggingMessageNotificationSchema } from '../../types.js';
|
||||
/**
|
||||
* Simplified Backwards Compatible MCP Client
|
||||
*
|
||||
* This client demonstrates backward compatibility with both:
|
||||
* 1. Modern servers using Streamable HTTP transport (protocol version 2025-03-26)
|
||||
* 2. Older servers using HTTP+SSE transport (protocol version 2024-11-05)
|
||||
*
|
||||
* Following the MCP specification for backwards compatibility:
|
||||
* - Attempts to POST an initialize request to the server URL first (modern transport)
|
||||
* - If that fails with 4xx status, falls back to GET request for SSE stream (older transport)
|
||||
*/
|
||||
// Command line args processing
|
||||
const args = process.argv.slice(2);
|
||||
const serverUrl = args[0] || 'http://localhost:3000/mcp';
|
||||
async function main() {
|
||||
console.log('MCP Backwards Compatible Client');
|
||||
console.log('===============================');
|
||||
console.log(`Connecting to server at: ${serverUrl}`);
|
||||
let client;
|
||||
let transport;
|
||||
try {
|
||||
// Try connecting with automatic transport detection
|
||||
const connection = await connectWithBackwardsCompatibility(serverUrl);
|
||||
client = connection.client;
|
||||
transport = connection.transport;
|
||||
// Set up notification handler
|
||||
client.setNotificationHandler(LoggingMessageNotificationSchema, notification => {
|
||||
console.log(`Notification: ${notification.params.level} - ${notification.params.data}`);
|
||||
});
|
||||
// DEMO WORKFLOW:
|
||||
// 1. List available tools
|
||||
console.log('\n=== Listing Available Tools ===');
|
||||
await listTools(client);
|
||||
// 2. Call the notification tool
|
||||
console.log('\n=== Starting Notification Stream ===');
|
||||
await startNotificationTool(client);
|
||||
// 3. Wait for all notifications (5 seconds)
|
||||
console.log('\n=== Waiting for all notifications ===');
|
||||
await new Promise(resolve => setTimeout(resolve, 5000));
|
||||
// 4. Disconnect
|
||||
console.log('\n=== Disconnecting ===');
|
||||
await transport.close();
|
||||
console.log('Disconnected from MCP server');
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error running client:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Connect to an MCP server with backwards compatibility
|
||||
* Following the spec for client backward compatibility
|
||||
*/
|
||||
async function connectWithBackwardsCompatibility(url) {
|
||||
console.log('1. Trying Streamable HTTP transport first...');
|
||||
// Step 1: Try Streamable HTTP transport first
|
||||
const client = new Client({
|
||||
name: 'backwards-compatible-client',
|
||||
version: '1.0.0'
|
||||
});
|
||||
client.onerror = error => {
|
||||
console.error('Client error:', error);
|
||||
};
|
||||
const baseUrl = new URL(url);
|
||||
try {
|
||||
// Create modern transport
|
||||
const streamableTransport = new StreamableHTTPClientTransport(baseUrl);
|
||||
await client.connect(streamableTransport);
|
||||
console.log('Successfully connected using modern Streamable HTTP transport.');
|
||||
return {
|
||||
client,
|
||||
transport: streamableTransport,
|
||||
transportType: 'streamable-http'
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
// Step 2: If transport fails, try the older SSE transport
|
||||
console.log(`StreamableHttp transport connection failed: ${error}`);
|
||||
console.log('2. Falling back to deprecated HTTP+SSE transport...');
|
||||
try {
|
||||
// Create SSE transport pointing to /sse endpoint
|
||||
const sseTransport = new SSEClientTransport(baseUrl);
|
||||
const sseClient = new Client({
|
||||
name: 'backwards-compatible-client',
|
||||
version: '1.0.0'
|
||||
});
|
||||
await sseClient.connect(sseTransport);
|
||||
console.log('Successfully connected using deprecated HTTP+SSE transport.');
|
||||
return {
|
||||
client: sseClient,
|
||||
transport: sseTransport,
|
||||
transportType: 'sse'
|
||||
};
|
||||
}
|
||||
catch (sseError) {
|
||||
console.error(`Failed to connect with either transport method:\n1. Streamable HTTP error: ${error}\n2. SSE error: ${sseError}`);
|
||||
throw new Error('Could not connect to server with any available transport');
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* List available tools on the server
|
||||
*/
|
||||
async function listTools(client) {
|
||||
try {
|
||||
const toolsRequest = {
|
||||
method: 'tools/list',
|
||||
params: {}
|
||||
};
|
||||
const toolsResult = await client.request(toolsRequest, ListToolsResultSchema);
|
||||
console.log('Available tools:');
|
||||
if (toolsResult.tools.length === 0) {
|
||||
console.log(' No tools available');
|
||||
}
|
||||
else {
|
||||
for (const tool of toolsResult.tools) {
|
||||
console.log(` - ${tool.name}: ${tool.description}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.log(`Tools not supported by this server: ${error}`);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Start a notification stream by calling the notification tool
|
||||
*/
|
||||
async function startNotificationTool(client) {
|
||||
try {
|
||||
// Call the notification tool using reasonable defaults
|
||||
const request = {
|
||||
method: 'tools/call',
|
||||
params: {
|
||||
name: 'start-notification-stream',
|
||||
arguments: {
|
||||
interval: 1000, // 1 second between notifications
|
||||
count: 5 // Send 5 notifications
|
||||
}
|
||||
}
|
||||
};
|
||||
console.log('Calling notification tool...');
|
||||
const result = await client.request(request, CallToolResultSchema);
|
||||
console.log('Tool result:');
|
||||
result.content.forEach(item => {
|
||||
if (item.type === 'text') {
|
||||
console.log(` ${item.text}`);
|
||||
}
|
||||
else {
|
||||
console.log(` ${item.type} content:`, item);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
console.log(`Error calling notification tool: ${error}`);
|
||||
}
|
||||
}
|
||||
// Start the client
|
||||
main().catch((error) => {
|
||||
console.error('Error running MCP client:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
//# sourceMappingURL=streamableHttpWithSseFallbackClient.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/streamableHttpWithSseFallbackClient.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/client/streamableHttpWithSseFallbackClient.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"streamableHttpWithSseFallbackClient.js","sourceRoot":"","sources":["../../../../src/examples/client/streamableHttpWithSseFallbackClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,6BAA6B,EAAE,MAAM,gCAAgC,CAAC;AAC/E,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAEH,qBAAqB,EAErB,oBAAoB,EACpB,gCAAgC,EACnC,MAAM,gBAAgB,CAAC;AAExB;;;;;;;;;;GAUG;AAEH,+BAA+B;AAC/B,MAAM,IAAI,GAAG,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sdk/dist/esm/examples/server/demoInMemoryOAuthProvider.d.ts
generated
vendored
Normal file
78
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/demoInMemoryOAuthProvider.d.ts
generated
vendored
Normal file
@@ -0,0 +1,78 @@
|
||||
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 { AuthInfo } from '../../server/auth/types.js';
|
||||
export declare class DemoInMemoryClientsStore implements OAuthRegisteredClientsStore {
|
||||
private clients;
|
||||
getClient(clientId: string): Promise<{
|
||||
redirect_uris: string[];
|
||||
client_id: string;
|
||||
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;
|
||||
software_statement?: string | undefined;
|
||||
client_secret?: string | undefined;
|
||||
client_id_issued_at?: number | undefined;
|
||||
client_secret_expires_at?: number | undefined;
|
||||
} | undefined>;
|
||||
registerClient(clientMetadata: OAuthClientInformationFull): Promise<{
|
||||
redirect_uris: string[];
|
||||
client_id: string;
|
||||
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;
|
||||
software_statement?: string | undefined;
|
||||
client_secret?: string | undefined;
|
||||
client_id_issued_at?: number | undefined;
|
||||
client_secret_expires_at?: number | undefined;
|
||||
}>;
|
||||
}
|
||||
/**
|
||||
* 🚨 DEMO ONLY - NOT FOR PRODUCTION
|
||||
*
|
||||
* This example demonstrates MCP OAuth flow but lacks some of the features required for production use,
|
||||
* for example:
|
||||
* - Persistent token storage
|
||||
* - Rate limiting
|
||||
*/
|
||||
export declare class DemoInMemoryAuthProvider implements OAuthServerProvider {
|
||||
private validateResource?;
|
||||
clientsStore: DemoInMemoryClientsStore;
|
||||
private codes;
|
||||
private tokens;
|
||||
constructor(validateResource?: ((resource?: URL) => boolean) | undefined);
|
||||
authorize(client: OAuthClientInformationFull, params: AuthorizationParams, res: Response): Promise<void>;
|
||||
challengeForAuthorizationCode(client: OAuthClientInformationFull, authorizationCode: string): Promise<string>;
|
||||
exchangeAuthorizationCode(client: OAuthClientInformationFull, authorizationCode: string, _codeVerifier?: string): Promise<OAuthTokens>;
|
||||
exchangeRefreshToken(_client: OAuthClientInformationFull, _refreshToken: string, _scopes?: string[], _resource?: URL): Promise<OAuthTokens>;
|
||||
verifyAccessToken(token: string): Promise<AuthInfo>;
|
||||
}
|
||||
export declare const setupAuthServer: ({ authServerUrl, mcpServerUrl, strictResource }: {
|
||||
authServerUrl: URL;
|
||||
mcpServerUrl: URL;
|
||||
strictResource: boolean;
|
||||
}) => OAuthMetadata;
|
||||
//# sourceMappingURL=demoInMemoryOAuthProvider.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/demoInMemoryOAuthProvider.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/demoInMemoryOAuthProvider.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"demoInMemoryOAuthProvider.d.ts","sourceRoot":"","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B,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"}
|
||||
196
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/demoInMemoryOAuthProvider.js
generated
vendored
Normal file
196
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/demoInMemoryOAuthProvider.js
generated
vendored
Normal file
@@ -0,0 +1,196 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
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();
|
||||
}
|
||||
async getClient(clientId) {
|
||||
return this.clients.get(clientId);
|
||||
}
|
||||
async registerClient(clientMetadata) {
|
||||
this.clients.set(clientMetadata.client_id, clientMetadata);
|
||||
return clientMetadata;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 🚨 DEMO ONLY - NOT FOR PRODUCTION
|
||||
*
|
||||
* This example demonstrates MCP OAuth flow but lacks some of the features required for production use,
|
||||
* for example:
|
||||
* - Persistent token storage
|
||||
* - Rate limiting
|
||||
*/
|
||||
export class DemoInMemoryAuthProvider {
|
||||
constructor(validateResource) {
|
||||
this.validateResource = validateResource;
|
||||
this.clientsStore = new DemoInMemoryClientsStore();
|
||||
this.codes = new Map();
|
||||
this.tokens = new Map();
|
||||
}
|
||||
async authorize(client, params, res) {
|
||||
const code = randomUUID();
|
||||
const searchParams = new URLSearchParams({
|
||||
code
|
||||
});
|
||||
if (params.state !== undefined) {
|
||||
searchParams.set('state', params.state);
|
||||
}
|
||||
this.codes.set(code, {
|
||||
client,
|
||||
params
|
||||
});
|
||||
// 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());
|
||||
}
|
||||
async challengeForAuthorizationCode(client, authorizationCode) {
|
||||
// Store the challenge with the code data
|
||||
const codeData = this.codes.get(authorizationCode);
|
||||
if (!codeData) {
|
||||
throw new Error('Invalid authorization code');
|
||||
}
|
||||
return codeData.params.codeChallenge;
|
||||
}
|
||||
async exchangeAuthorizationCode(client, authorizationCode,
|
||||
// Note: code verifier is checked in token.ts by default
|
||||
// it's unused here for that reason.
|
||||
_codeVerifier) {
|
||||
const codeData = this.codes.get(authorizationCode);
|
||||
if (!codeData) {
|
||||
throw new Error('Invalid authorization code');
|
||||
}
|
||||
if (codeData.client.client_id !== client.client_id) {
|
||||
throw new Error(`Authorization code was not issued to this client, ${codeData.client.client_id} != ${client.client_id}`);
|
||||
}
|
||||
if (this.validateResource && !this.validateResource(codeData.params.resource)) {
|
||||
throw new Error(`Invalid resource: ${codeData.params.resource}`);
|
||||
}
|
||||
this.codes.delete(authorizationCode);
|
||||
const token = randomUUID();
|
||||
const tokenData = {
|
||||
token,
|
||||
clientId: client.client_id,
|
||||
scopes: codeData.params.scopes || [],
|
||||
expiresAt: Date.now() + 3600000, // 1 hour
|
||||
resource: codeData.params.resource,
|
||||
type: 'access'
|
||||
};
|
||||
this.tokens.set(token, tokenData);
|
||||
return {
|
||||
access_token: token,
|
||||
token_type: 'bearer',
|
||||
expires_in: 3600,
|
||||
scope: (codeData.params.scopes || []).join(' ')
|
||||
};
|
||||
}
|
||||
async exchangeRefreshToken(_client, _refreshToken, _scopes, _resource) {
|
||||
throw new Error('Not implemented for example demo');
|
||||
}
|
||||
async verifyAccessToken(token) {
|
||||
const tokenData = this.tokens.get(token);
|
||||
if (!tokenData || !tokenData.expiresAt || tokenData.expiresAt < Date.now()) {
|
||||
throw new Error('Invalid or expired token');
|
||||
}
|
||||
return {
|
||||
token,
|
||||
clientId: tokenData.clientId,
|
||||
scopes: tokenData.scopes,
|
||||
expiresAt: Math.floor(tokenData.expiresAt / 1000),
|
||||
resource: tokenData.resource
|
||||
};
|
||||
}
|
||||
}
|
||||
export const setupAuthServer = ({ authServerUrl, mcpServerUrl, strictResource }) => {
|
||||
// Create separate auth server app
|
||||
// NOTE: This is a separate app on a separate port to illustrate
|
||||
// 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 provider = new DemoInMemoryAuthProvider(validateResource);
|
||||
const authApp = express();
|
||||
authApp.use(express.json());
|
||||
// For introspection requests
|
||||
authApp.use(express.urlencoded());
|
||||
// Add OAuth routes to the auth server
|
||||
// NOTE: this will also add a protected resource metadata route,
|
||||
// but it won't be used, so leave it.
|
||||
authApp.use(mcpAuthRouter({
|
||||
provider,
|
||||
issuerUrl: authServerUrl,
|
||||
scopesSupported: ['mcp:tools']
|
||||
}));
|
||||
authApp.post('/introspect', async (req, res) => {
|
||||
try {
|
||||
const { token } = req.body;
|
||||
if (!token) {
|
||||
res.status(400).json({ error: 'Token is required' });
|
||||
return;
|
||||
}
|
||||
const tokenInfo = await provider.verifyAccessToken(token);
|
||||
res.json({
|
||||
active: true,
|
||||
client_id: tokenInfo.clientId,
|
||||
scope: tokenInfo.scopes.join(' '),
|
||||
exp: tokenInfo.expiresAt,
|
||||
aud: tokenInfo.resource
|
||||
});
|
||||
return;
|
||||
}
|
||||
catch (error) {
|
||||
res.status(401).json({
|
||||
active: false,
|
||||
error: 'Unauthorized',
|
||||
error_description: `Invalid token: ${error}`
|
||||
});
|
||||
}
|
||||
});
|
||||
const auth_port = authServerUrl.port;
|
||||
// Start the auth server
|
||||
authApp.listen(auth_port, error => {
|
||||
if (error) {
|
||||
console.error('Failed to start server:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(`OAuth Authorization Server listening on port ${auth_port}`);
|
||||
});
|
||||
// Note: we could fetch this from the server, but then we end up
|
||||
// with some top level async which gets annoying.
|
||||
const oauthMetadata = createOAuthMetadata({
|
||||
provider,
|
||||
issuerUrl: authServerUrl,
|
||||
scopesSupported: ['mcp:tools']
|
||||
});
|
||||
oauthMetadata.introspection_endpoint = new URL('/introspect', authServerUrl).href;
|
||||
return oauthMetadata;
|
||||
};
|
||||
//# sourceMappingURL=demoInMemoryOAuthProvider.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/demoInMemoryOAuthProvider.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/demoInMemoryOAuthProvider.js.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/elicitationFormExample.d.ts
generated
vendored
Normal file
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/elicitationFormExample.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=elicitationFormExample.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/elicitationFormExample.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/elicitationFormExample.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"elicitationFormExample.d.ts","sourceRoot":"","sources":["../../../../src/examples/server/elicitationFormExample.ts"],"names":[],"mappings":""}
|
||||
458
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/elicitationFormExample.js
generated
vendored
Normal file
458
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/elicitationFormExample.js
generated
vendored
Normal file
@@ -0,0 +1,458 @@
|
||||
// Run with: npx tsx src/examples/server/elicitationFormExample.ts
|
||||
//
|
||||
// This example demonstrates how to use form elicitation to collect structured user input
|
||||
// with JSON Schema validation via a local HTTP server with SSE streaming.
|
||||
// Form elicitation allows servers to request *non-sensitive* user input through the client
|
||||
// with schema-based validation.
|
||||
// Note: See also elicitationUrlExample.ts for an example of using URL elicitation
|
||||
// to collect *sensitive* user input via a browser.
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { McpServer } from '../../server/mcp.js';
|
||||
import { StreamableHTTPServerTransport } from '../../server/streamableHttp.js';
|
||||
import { isInitializeRequest } from '../../types.js';
|
||||
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 = () => {
|
||||
// Create MCP server - it will automatically use AjvJsonSchemaValidator with sensible defaults
|
||||
// The validator supports format validation (email, date, etc.) if ajv-formats is installed
|
||||
const mcpServer = new McpServer({
|
||||
name: 'form-elicitation-example-server',
|
||||
version: '1.0.0'
|
||||
}, {
|
||||
capabilities: {}
|
||||
});
|
||||
/**
|
||||
* Example 1: Simple user registration tool
|
||||
* Collects username, email, and password from the user
|
||||
*/
|
||||
mcpServer.registerTool('register_user', {
|
||||
description: 'Register a new user account by collecting their information',
|
||||
inputSchema: {}
|
||||
}, async () => {
|
||||
try {
|
||||
// Request user information through form elicitation
|
||||
const result = await mcpServer.server.elicitInput({
|
||||
mode: 'form',
|
||||
message: 'Please provide your registration information:',
|
||||
requestedSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
username: {
|
||||
type: 'string',
|
||||
title: 'Username',
|
||||
description: 'Your desired username (3-20 characters)',
|
||||
minLength: 3,
|
||||
maxLength: 20
|
||||
},
|
||||
email: {
|
||||
type: 'string',
|
||||
title: 'Email',
|
||||
description: 'Your email address',
|
||||
format: 'email'
|
||||
},
|
||||
password: {
|
||||
type: 'string',
|
||||
title: 'Password',
|
||||
description: 'Your password (min 8 characters)',
|
||||
minLength: 8
|
||||
},
|
||||
newsletter: {
|
||||
type: 'boolean',
|
||||
title: 'Newsletter',
|
||||
description: 'Subscribe to newsletter?',
|
||||
default: false
|
||||
},
|
||||
role: {
|
||||
type: 'string',
|
||||
title: 'Role',
|
||||
description: 'Your primary role',
|
||||
oneOf: [
|
||||
{ const: 'developer', title: 'Developer' },
|
||||
{ const: 'designer', title: 'Designer' },
|
||||
{ const: 'manager', title: 'Manager' },
|
||||
{ const: 'other', title: 'Other' }
|
||||
],
|
||||
default: 'developer'
|
||||
},
|
||||
interests: {
|
||||
type: 'array',
|
||||
title: 'Interests',
|
||||
description: 'Select your areas of interest',
|
||||
items: {
|
||||
type: 'string',
|
||||
enum: ['frontend', 'backend', 'mobile', 'devops', 'ai']
|
||||
},
|
||||
minItems: 1,
|
||||
maxItems: 3
|
||||
}
|
||||
},
|
||||
required: ['username', 'email', 'password']
|
||||
}
|
||||
});
|
||||
// Handle the different possible actions
|
||||
if (result.action === 'accept' && result.content) {
|
||||
const { username, email, newsletter } = result.content;
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Registration successful!\n\nUsername: ${username}\nEmail: ${email}\nNewsletter: ${newsletter ? 'Yes' : 'No'}`
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
else if (result.action === 'decline') {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: 'Registration cancelled by user.'
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
else {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: 'Registration was cancelled.'
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Registration failed: ${error instanceof Error ? error.message : String(error)}`
|
||||
}
|
||||
],
|
||||
isError: true
|
||||
};
|
||||
}
|
||||
});
|
||||
/**
|
||||
* Example 2: Multi-step workflow with multiple form elicitation requests
|
||||
* Demonstrates how to collect information in multiple steps
|
||||
*/
|
||||
mcpServer.registerTool('create_event', {
|
||||
description: 'Create a calendar event by collecting event details',
|
||||
inputSchema: {}
|
||||
}, async () => {
|
||||
try {
|
||||
// Step 1: Collect basic event information
|
||||
const basicInfo = await mcpServer.server.elicitInput({
|
||||
mode: 'form',
|
||||
message: 'Step 1: Enter basic event information',
|
||||
requestedSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
title: {
|
||||
type: 'string',
|
||||
title: 'Event Title',
|
||||
description: 'Name of the event',
|
||||
minLength: 1
|
||||
},
|
||||
description: {
|
||||
type: 'string',
|
||||
title: 'Description',
|
||||
description: 'Event description (optional)'
|
||||
}
|
||||
},
|
||||
required: ['title']
|
||||
}
|
||||
});
|
||||
if (basicInfo.action !== 'accept' || !basicInfo.content) {
|
||||
return {
|
||||
content: [{ type: 'text', text: 'Event creation cancelled.' }]
|
||||
};
|
||||
}
|
||||
// Step 2: Collect date and time
|
||||
const dateTime = await mcpServer.server.elicitInput({
|
||||
mode: 'form',
|
||||
message: 'Step 2: Enter date and time',
|
||||
requestedSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
date: {
|
||||
type: 'string',
|
||||
title: 'Date',
|
||||
description: 'Event date',
|
||||
format: 'date'
|
||||
},
|
||||
startTime: {
|
||||
type: 'string',
|
||||
title: 'Start Time',
|
||||
description: 'Event start time (HH:MM)'
|
||||
},
|
||||
duration: {
|
||||
type: 'integer',
|
||||
title: 'Duration',
|
||||
description: 'Duration in minutes',
|
||||
minimum: 15,
|
||||
maximum: 480
|
||||
}
|
||||
},
|
||||
required: ['date', 'startTime', 'duration']
|
||||
}
|
||||
});
|
||||
if (dateTime.action !== 'accept' || !dateTime.content) {
|
||||
return {
|
||||
content: [{ type: 'text', text: 'Event creation cancelled.' }]
|
||||
};
|
||||
}
|
||||
// Combine all collected information
|
||||
const event = {
|
||||
...basicInfo.content,
|
||||
...dateTime.content
|
||||
};
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Event created successfully!\n\n${JSON.stringify(event, null, 2)}`
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Event creation failed: ${error instanceof Error ? error.message : String(error)}`
|
||||
}
|
||||
],
|
||||
isError: true
|
||||
};
|
||||
}
|
||||
});
|
||||
/**
|
||||
* Example 3: Collecting address information
|
||||
* Demonstrates validation with patterns and optional fields
|
||||
*/
|
||||
mcpServer.registerTool('update_shipping_address', {
|
||||
description: 'Update shipping address with validation',
|
||||
inputSchema: {}
|
||||
}, async () => {
|
||||
try {
|
||||
const result = await mcpServer.server.elicitInput({
|
||||
mode: 'form',
|
||||
message: 'Please provide your shipping address:',
|
||||
requestedSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: {
|
||||
type: 'string',
|
||||
title: 'Full Name',
|
||||
description: 'Recipient name',
|
||||
minLength: 1
|
||||
},
|
||||
street: {
|
||||
type: 'string',
|
||||
title: 'Street Address',
|
||||
minLength: 1
|
||||
},
|
||||
city: {
|
||||
type: 'string',
|
||||
title: 'City',
|
||||
minLength: 1
|
||||
},
|
||||
state: {
|
||||
type: 'string',
|
||||
title: 'State/Province',
|
||||
minLength: 2,
|
||||
maxLength: 2
|
||||
},
|
||||
zipCode: {
|
||||
type: 'string',
|
||||
title: 'ZIP/Postal Code',
|
||||
description: '5-digit ZIP code'
|
||||
},
|
||||
phone: {
|
||||
type: 'string',
|
||||
title: 'Phone Number (optional)',
|
||||
description: 'Contact phone number'
|
||||
}
|
||||
},
|
||||
required: ['name', 'street', 'city', 'state', 'zipCode']
|
||||
}
|
||||
});
|
||||
if (result.action === 'accept' && result.content) {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Address updated successfully!\n\n${JSON.stringify(result.content, null, 2)}`
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
else if (result.action === 'decline') {
|
||||
return {
|
||||
content: [{ type: 'text', text: 'Address update cancelled by user.' }]
|
||||
};
|
||||
}
|
||||
else {
|
||||
return {
|
||||
content: [{ type: 'text', text: 'Address update was cancelled.' }]
|
||||
};
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Address update failed: ${error instanceof Error ? error.message : String(error)}`
|
||||
}
|
||||
],
|
||||
isError: true
|
||||
};
|
||||
}
|
||||
});
|
||||
return mcpServer;
|
||||
};
|
||||
async function main() {
|
||||
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3000;
|
||||
const app = createMcpExpressApp();
|
||||
// Map to store transports by session ID
|
||||
const transports = {};
|
||||
// MCP POST endpoint
|
||||
const mcpPostHandler = async (req, res) => {
|
||||
const sessionId = req.headers['mcp-session-id'];
|
||||
if (sessionId) {
|
||||
console.log(`Received MCP request for session: ${sessionId}`);
|
||||
}
|
||||
try {
|
||||
let transport;
|
||||
if (sessionId && transports[sessionId]) {
|
||||
// Reuse existing transport for this session
|
||||
transport = transports[sessionId];
|
||||
}
|
||||
else if (!sessionId && isInitializeRequest(req.body)) {
|
||||
// New initialization request - create new transport
|
||||
transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: () => randomUUID(),
|
||||
onsessioninitialized: sessionId => {
|
||||
// Store the transport by session ID when session is initialized
|
||||
console.log(`Session initialized with ID: ${sessionId}`);
|
||||
transports[sessionId] = transport;
|
||||
}
|
||||
});
|
||||
// Set up onclose handler to clean up transport when closed
|
||||
transport.onclose = () => {
|
||||
const sid = transport.sessionId;
|
||||
if (sid && transports[sid]) {
|
||||
console.log(`Transport closed for session ${sid}, removing from transports map`);
|
||||
delete transports[sid];
|
||||
}
|
||||
};
|
||||
// Create a new server per session and connect it to the transport
|
||||
const mcpServer = getServer();
|
||||
await mcpServer.connect(transport);
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
// Invalid request - no session ID or not initialization request
|
||||
res.status(400).json({
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32000,
|
||||
message: 'Bad Request: No valid session ID provided'
|
||||
},
|
||||
id: null
|
||||
});
|
||||
return;
|
||||
}
|
||||
// Handle the request with existing transport
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error handling MCP request:', error);
|
||||
if (!res.headersSent) {
|
||||
res.status(500).json({
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32603,
|
||||
message: 'Internal server error'
|
||||
},
|
||||
id: null
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
app.post('/mcp', mcpPostHandler);
|
||||
// Handle GET requests for SSE streams
|
||||
const mcpGetHandler = async (req, res) => {
|
||||
const sessionId = req.headers['mcp-session-id'];
|
||||
if (!sessionId || !transports[sessionId]) {
|
||||
res.status(400).send('Invalid or missing session ID');
|
||||
return;
|
||||
}
|
||||
console.log(`Establishing SSE stream for session ${sessionId}`);
|
||||
const transport = transports[sessionId];
|
||||
await transport.handleRequest(req, res);
|
||||
};
|
||||
app.get('/mcp', mcpGetHandler);
|
||||
// Handle DELETE requests for session termination
|
||||
const mcpDeleteHandler = async (req, res) => {
|
||||
const sessionId = req.headers['mcp-session-id'];
|
||||
if (!sessionId || !transports[sessionId]) {
|
||||
res.status(400).send('Invalid or missing session ID');
|
||||
return;
|
||||
}
|
||||
console.log(`Received session termination request for session ${sessionId}`);
|
||||
try {
|
||||
const transport = transports[sessionId];
|
||||
await transport.handleRequest(req, res);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error handling session termination:', error);
|
||||
if (!res.headersSent) {
|
||||
res.status(500).send('Error processing session termination');
|
||||
}
|
||||
}
|
||||
};
|
||||
app.delete('/mcp', mcpDeleteHandler);
|
||||
// Start listening
|
||||
app.listen(PORT, error => {
|
||||
if (error) {
|
||||
console.error('Failed to start server:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(`Form elicitation example server is running on http://localhost:${PORT}/mcp`);
|
||||
console.log('Available tools:');
|
||||
console.log(' - register_user: Collect user registration information');
|
||||
console.log(' - create_event: Multi-step event creation');
|
||||
console.log(' - update_shipping_address: Collect and validate address');
|
||||
console.log('\nConnect your MCP client to this server using the HTTP transport.');
|
||||
});
|
||||
// Handle server shutdown
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('Shutting down server...');
|
||||
// Close all active transports to properly clean up resources
|
||||
for (const sessionId in transports) {
|
||||
try {
|
||||
console.log(`Closing transport for session ${sessionId}`);
|
||||
await transports[sessionId].close();
|
||||
delete transports[sessionId];
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`Error closing transport for session ${sessionId}:`, error);
|
||||
}
|
||||
}
|
||||
console.log('Server shutdown complete');
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
main().catch(error => {
|
||||
console.error('Server error:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
//# sourceMappingURL=elicitationFormExample.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/elicitationFormExample.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/elicitationFormExample.js.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/elicitationUrlExample.d.ts
generated
vendored
Normal file
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/elicitationUrlExample.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=elicitationUrlExample.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/elicitationUrlExample.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/elicitationUrlExample.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"elicitationUrlExample.d.ts","sourceRoot":"","sources":["../../../../src/examples/server/elicitationUrlExample.ts"],"names":[],"mappings":""}
|
||||
651
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/elicitationUrlExample.js
generated
vendored
Normal file
651
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/elicitationUrlExample.js
generated
vendored
Normal file
@@ -0,0 +1,651 @@
|
||||
// Run with: npx tsx src/examples/server/elicitationUrlExample.ts
|
||||
//
|
||||
// This example demonstrates how to use URL elicitation to securely collect
|
||||
// *sensitive* user input in a remote (HTTP) server.
|
||||
// URL elicitation allows servers to prompt the end-user to open a URL in their browser
|
||||
// to collect sensitive information.
|
||||
// Note: See also elicitationFormExample.ts for an example of using form (not URL) elicitation
|
||||
// to collect *non-sensitive* user input with a structured schema.
|
||||
import express from 'express';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { z } from 'zod';
|
||||
import { McpServer } from '../../server/mcp.js';
|
||||
import { createMcpExpressApp } from '../../server/express.js';
|
||||
import { StreamableHTTPServerTransport } from '../../server/streamableHttp.js';
|
||||
import { getOAuthProtectedResourceMetadataUrl, mcpAuthMetadataRouter } from '../../server/auth/router.js';
|
||||
import { requireBearerAuth } from '../../server/auth/middleware/bearerAuth.js';
|
||||
import { UrlElicitationRequiredError, isInitializeRequest } from '../../types.js';
|
||||
import { InMemoryEventStore } from '../shared/inMemoryEventStore.js';
|
||||
import { setupAuthServer } from './demoInMemoryOAuthProvider.js';
|
||||
import { checkResourceAllowed } from '../../shared/auth-utils.js';
|
||||
import cors from 'cors';
|
||||
// Create an MCP server with implementation details
|
||||
const getServer = () => {
|
||||
const mcpServer = new McpServer({
|
||||
name: 'url-elicitation-http-server',
|
||||
version: '1.0.0'
|
||||
}, {
|
||||
capabilities: { logging: {} }
|
||||
});
|
||||
mcpServer.registerTool('payment-confirm', {
|
||||
description: 'A tool that confirms a payment directly with a user',
|
||||
inputSchema: {
|
||||
cartId: z.string().describe('The ID of the cart to confirm')
|
||||
}
|
||||
}, async ({ cartId }, extra) => {
|
||||
/*
|
||||
In a real world scenario, there would be some logic here to check if the user has the provided cartId.
|
||||
For the purposes of this example, we'll throw an error (-> elicits the client to open a URL to confirm payment)
|
||||
*/
|
||||
const sessionId = extra.sessionId;
|
||||
if (!sessionId) {
|
||||
throw new Error('Expected a Session ID');
|
||||
}
|
||||
// Create and track the elicitation
|
||||
const elicitationId = generateTrackedElicitation(sessionId, elicitationId => mcpServer.server.createElicitationCompletionNotifier(elicitationId));
|
||||
throw new UrlElicitationRequiredError([
|
||||
{
|
||||
mode: 'url',
|
||||
message: 'This tool requires a payment confirmation. Open the link to confirm payment!',
|
||||
url: `http://localhost:${MCP_PORT}/confirm-payment?session=${sessionId}&elicitation=${elicitationId}&cartId=${encodeURIComponent(cartId)}`,
|
||||
elicitationId
|
||||
}
|
||||
]);
|
||||
});
|
||||
mcpServer.registerTool('third-party-auth', {
|
||||
description: 'A demo tool that requires third-party OAuth credentials',
|
||||
inputSchema: {
|
||||
param1: z.string().describe('First parameter')
|
||||
}
|
||||
}, async (_, extra) => {
|
||||
/*
|
||||
In a real world scenario, there would be some logic here to check if we already have a valid access token for the user.
|
||||
Auth info (with a subject or `sub` claim) can be typically be found in `extra.authInfo`.
|
||||
If we do, we can just return the result of the tool call.
|
||||
If we don't, we can throw an ElicitationRequiredError to request the user to authenticate.
|
||||
For the purposes of this example, we'll throw an error (-> elicits the client to open a URL to authenticate).
|
||||
*/
|
||||
const sessionId = extra.sessionId;
|
||||
if (!sessionId) {
|
||||
throw new Error('Expected a Session ID');
|
||||
}
|
||||
// Create and track the elicitation
|
||||
const elicitationId = generateTrackedElicitation(sessionId, elicitationId => mcpServer.server.createElicitationCompletionNotifier(elicitationId));
|
||||
// Simulate OAuth callback and token exchange after 5 seconds
|
||||
// In a real app, this would be called from your OAuth callback handler
|
||||
setTimeout(() => {
|
||||
console.log(`Simulating OAuth token received for elicitation ${elicitationId}`);
|
||||
completeURLElicitation(elicitationId);
|
||||
}, 5000);
|
||||
throw new UrlElicitationRequiredError([
|
||||
{
|
||||
mode: 'url',
|
||||
message: 'This tool requires access to your example.com account. Open the link to authenticate!',
|
||||
url: 'https://www.example.com/oauth/authorize',
|
||||
elicitationId
|
||||
}
|
||||
]);
|
||||
});
|
||||
return mcpServer;
|
||||
};
|
||||
const elicitationsMap = new Map();
|
||||
// Clean up old elicitations after 1 hour to prevent memory leaks
|
||||
const ELICITATION_TTL_MS = 60 * 60 * 1000; // 1 hour
|
||||
const CLEANUP_INTERVAL_MS = 10 * 60 * 1000; // 10 minutes
|
||||
function cleanupOldElicitations() {
|
||||
const now = new Date();
|
||||
for (const [id, metadata] of elicitationsMap.entries()) {
|
||||
if (now.getTime() - metadata.createdAt.getTime() > ELICITATION_TTL_MS) {
|
||||
elicitationsMap.delete(id);
|
||||
console.log(`Cleaned up expired elicitation: ${id}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
setInterval(cleanupOldElicitations, CLEANUP_INTERVAL_MS);
|
||||
/**
|
||||
* Elicitation IDs must be unique strings within the MCP session
|
||||
* UUIDs are used in this example for simplicity
|
||||
*/
|
||||
function generateElicitationId() {
|
||||
return randomUUID();
|
||||
}
|
||||
/**
|
||||
* Helper function to create and track a new elicitation.
|
||||
*/
|
||||
function generateTrackedElicitation(sessionId, createCompletionNotifier) {
|
||||
const elicitationId = generateElicitationId();
|
||||
// Create a Promise and its resolver for tracking completion
|
||||
let completeResolver;
|
||||
const completedPromise = new Promise(resolve => {
|
||||
completeResolver = resolve;
|
||||
});
|
||||
const completionNotifier = createCompletionNotifier ? createCompletionNotifier(elicitationId) : undefined;
|
||||
// Store the elicitation in our map
|
||||
elicitationsMap.set(elicitationId, {
|
||||
status: 'pending',
|
||||
completedPromise,
|
||||
completeResolver: completeResolver,
|
||||
createdAt: new Date(),
|
||||
sessionId,
|
||||
completionNotifier
|
||||
});
|
||||
return elicitationId;
|
||||
}
|
||||
/**
|
||||
* Helper function to complete an elicitation.
|
||||
*/
|
||||
function completeURLElicitation(elicitationId) {
|
||||
const elicitation = elicitationsMap.get(elicitationId);
|
||||
if (!elicitation) {
|
||||
console.warn(`Attempted to complete unknown elicitation: ${elicitationId}`);
|
||||
return;
|
||||
}
|
||||
if (elicitation.status === 'complete') {
|
||||
console.warn(`Elicitation already complete: ${elicitationId}`);
|
||||
return;
|
||||
}
|
||||
// Update metadata
|
||||
elicitation.status = 'complete';
|
||||
// Send completion notification to the client
|
||||
if (elicitation.completionNotifier) {
|
||||
console.log(`Sending notifications/elicitation/complete notification for elicitation ${elicitationId}`);
|
||||
elicitation.completionNotifier().catch(error => {
|
||||
console.error(`Failed to send completion notification for elicitation ${elicitationId}:`, error);
|
||||
});
|
||||
}
|
||||
// Resolve the promise to unblock any waiting code
|
||||
elicitation.completeResolver();
|
||||
}
|
||||
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 = createMcpExpressApp();
|
||||
// Allow CORS all domains, expose the Mcp-Session-Id header
|
||||
app.use(cors({
|
||||
origin: '*', // Allow all origins
|
||||
exposedHeaders: ['Mcp-Session-Id'],
|
||||
credentials: true // Allow cookies to be sent cross-origin
|
||||
}));
|
||||
// Set up OAuth (required for this example)
|
||||
let authMiddleware = null;
|
||||
// Create auth middleware for MCP endpoints
|
||||
const mcpServerUrl = new URL(`http://localhost:${MCP_PORT}/mcp`);
|
||||
const authServerUrl = new URL(`http://localhost:${AUTH_PORT}`);
|
||||
const oauthMetadata = setupAuthServer({ authServerUrl, mcpServerUrl, strictResource: true });
|
||||
const tokenVerifier = {
|
||||
verifyAccessToken: async (token) => {
|
||||
const endpoint = oauthMetadata.introspection_endpoint;
|
||||
if (!endpoint) {
|
||||
throw new Error('No token verification endpoint available in metadata');
|
||||
}
|
||||
const response = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
token: token
|
||||
}).toString()
|
||||
});
|
||||
if (!response.ok) {
|
||||
const text = await response.text().catch(() => null);
|
||||
throw new Error(`Invalid or expired token: ${text}`);
|
||||
}
|
||||
const data = await response.json();
|
||||
if (!data.aud) {
|
||||
throw new Error(`Resource Indicator (RFC8707) missing`);
|
||||
}
|
||||
if (!checkResourceAllowed({ requestedResource: data.aud, configuredResource: mcpServerUrl })) {
|
||||
throw new Error(`Expected resource indicator ${mcpServerUrl}, got: ${data.aud}`);
|
||||
}
|
||||
// Convert the response to AuthInfo format
|
||||
return {
|
||||
token,
|
||||
clientId: data.client_id,
|
||||
scopes: data.scope ? data.scope.split(' ') : [],
|
||||
expiresAt: data.exp
|
||||
};
|
||||
}
|
||||
};
|
||||
// Add metadata routes to the main MCP server
|
||||
app.use(mcpAuthMetadataRouter({
|
||||
oauthMetadata,
|
||||
resourceServerUrl: mcpServerUrl,
|
||||
scopesSupported: ['mcp:tools'],
|
||||
resourceName: 'MCP Demo Server'
|
||||
}));
|
||||
authMiddleware = requireBearerAuth({
|
||||
verifier: tokenVerifier,
|
||||
requiredScopes: [],
|
||||
resourceMetadataUrl: getOAuthProtectedResourceMetadataUrl(mcpServerUrl)
|
||||
});
|
||||
/**
|
||||
* API Key Form Handling
|
||||
*
|
||||
* Many servers today require an API key to operate, but there's no scalable way to do this dynamically for remote servers within MCP protocol.
|
||||
* URL-mode elicitation enables the server to host a simple form and get the secret data securely from the user without involving the LLM or client.
|
||||
**/
|
||||
async function sendApiKeyElicitation(sessionId, sender, createCompletionNotifier) {
|
||||
if (!sessionId) {
|
||||
console.error('No session ID provided');
|
||||
throw new Error('Expected a Session ID to track elicitation');
|
||||
}
|
||||
console.log('🔑 URL elicitation demo: Requesting API key from client...');
|
||||
const elicitationId = generateTrackedElicitation(sessionId, createCompletionNotifier);
|
||||
try {
|
||||
const result = await sender({
|
||||
mode: 'url',
|
||||
message: 'Please provide your API key to authenticate with this server',
|
||||
// Host the form on the same server. In a real app, you might coordinate passing these state variables differently.
|
||||
url: `http://localhost:${MCP_PORT}/api-key-form?session=${sessionId}&elicitation=${elicitationId}`,
|
||||
elicitationId
|
||||
});
|
||||
switch (result.action) {
|
||||
case 'accept':
|
||||
console.log('🔑 URL elicitation demo: Client accepted the API key elicitation (now pending form submission)');
|
||||
// Wait for the API key to be submitted via the form
|
||||
// The form submission will complete the elicitation
|
||||
break;
|
||||
default:
|
||||
console.log('🔑 URL elicitation demo: Client declined to provide an API key');
|
||||
// In a real app, this might close the connection, but for the demo, we'll continue
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error during API key elicitation:', error);
|
||||
}
|
||||
}
|
||||
// API Key Form endpoint - serves a simple HTML form
|
||||
app.get('/api-key-form', (req, res) => {
|
||||
const mcpSessionId = req.query.session;
|
||||
const elicitationId = req.query.elicitation;
|
||||
if (!mcpSessionId || !elicitationId) {
|
||||
res.status(400).send('<h1>Error</h1><p>Missing required parameters</p>');
|
||||
return;
|
||||
}
|
||||
// Check for user session cookie
|
||||
// In production, this is often handled by some user auth middleware to ensure the user has a valid session
|
||||
// This session is different from the MCP session.
|
||||
// This userSession is the cookie that the MCP Server's Authorization Server sets for the user when they log in.
|
||||
const userSession = getUserSessionCookie(req.headers.cookie);
|
||||
if (!userSession) {
|
||||
res.status(401).send('<h1>Error</h1><p>Unauthorized - please reconnect to login again</p>');
|
||||
return;
|
||||
}
|
||||
// Serve a simple HTML form
|
||||
res.send(`
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Submit Your API Key</title>
|
||||
<style>
|
||||
body { font-family: sans-serif; max-width: 400px; margin: 50px auto; padding: 20px; }
|
||||
input[type="text"] { width: 100%; padding: 8px; margin: 10px 0; box-sizing: border-box; }
|
||||
button { background: #007bff; color: white; padding: 10px 20px; border: none; cursor: pointer; }
|
||||
button:hover { background: #0056b3; }
|
||||
.user { background: #d1ecf1; padding: 8px; margin-bottom: 10px; }
|
||||
.info { color: #666; font-size: 0.9em; margin-top: 20px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>API Key Required</h1>
|
||||
<div class="user">✓ Logged in as: <strong>${userSession.name}</strong></div>
|
||||
<form method="POST" action="/api-key-form">
|
||||
<input type="hidden" name="session" value="${mcpSessionId}" />
|
||||
<input type="hidden" name="elicitation" value="${elicitationId}" />
|
||||
<label>API Key:<br>
|
||||
<input type="text" name="apiKey" required placeholder="Enter your API key" />
|
||||
</label>
|
||||
<button type="submit">Submit</button>
|
||||
</form>
|
||||
<div class="info">This is a demo showing how a server can securely elicit sensitive data from a user using a URL.</div>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
});
|
||||
// Handle API key form submission
|
||||
app.post('/api-key-form', express.urlencoded(), (req, res) => {
|
||||
const { session: sessionId, apiKey, elicitation: elicitationId } = req.body;
|
||||
if (!sessionId || !apiKey || !elicitationId) {
|
||||
res.status(400).send('<h1>Error</h1><p>Missing required parameters</p>');
|
||||
return;
|
||||
}
|
||||
// Check for user session cookie here too
|
||||
const userSession = getUserSessionCookie(req.headers.cookie);
|
||||
if (!userSession) {
|
||||
res.status(401).send('<h1>Error</h1><p>Unauthorized - please reconnect to login again</p>');
|
||||
return;
|
||||
}
|
||||
// A real app might store this API key to be used later for the user.
|
||||
console.log(`🔑 Received API key \x1b[32m${apiKey}\x1b[0m for session ${sessionId}`);
|
||||
// If we have an elicitationId, complete the elicitation
|
||||
completeURLElicitation(elicitationId);
|
||||
// Send a success response
|
||||
res.send(`
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Success</title>
|
||||
<style>
|
||||
body { font-family: sans-serif; max-width: 400px; margin: 50px auto; padding: 20px; text-align: center; }
|
||||
.success { background: #d4edda; color: #155724; padding: 20px; margin: 20px 0; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="success">
|
||||
<h1>Success ✓</h1>
|
||||
<p>API key received.</p>
|
||||
</div>
|
||||
<p>You can close this window and return to your MCP client.</p>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
});
|
||||
// Helper to get the user session from the demo_session cookie
|
||||
function getUserSessionCookie(cookieHeader) {
|
||||
if (!cookieHeader)
|
||||
return null;
|
||||
const cookies = cookieHeader.split(';');
|
||||
for (const cookie of cookies) {
|
||||
const [name, value] = cookie.trim().split('=');
|
||||
if (name === 'demo_session' && value) {
|
||||
try {
|
||||
return JSON.parse(decodeURIComponent(value));
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Failed to parse demo_session cookie:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* Payment Confirmation Form Handling
|
||||
*
|
||||
* This demonstrates how a server can use URL-mode elicitation to get user confirmation
|
||||
* for sensitive operations like payment processing.
|
||||
**/
|
||||
// Payment Confirmation Form endpoint - serves a simple HTML form
|
||||
app.get('/confirm-payment', (req, res) => {
|
||||
const mcpSessionId = req.query.session;
|
||||
const elicitationId = req.query.elicitation;
|
||||
const cartId = req.query.cartId;
|
||||
if (!mcpSessionId || !elicitationId) {
|
||||
res.status(400).send('<h1>Error</h1><p>Missing required parameters</p>');
|
||||
return;
|
||||
}
|
||||
// Check for user session cookie
|
||||
// In production, this is often handled by some user auth middleware to ensure the user has a valid session
|
||||
// This session is different from the MCP session.
|
||||
// This userSession is the cookie that the MCP Server's Authorization Server sets for the user when they log in.
|
||||
const userSession = getUserSessionCookie(req.headers.cookie);
|
||||
if (!userSession) {
|
||||
res.status(401).send('<h1>Error</h1><p>Unauthorized - please reconnect to login again</p>');
|
||||
return;
|
||||
}
|
||||
// Serve a simple HTML form
|
||||
res.send(`
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Confirm Payment</title>
|
||||
<style>
|
||||
body { font-family: sans-serif; max-width: 400px; margin: 50px auto; padding: 20px; }
|
||||
button { background: #28a745; color: white; padding: 12px 24px; border: none; cursor: pointer; font-size: 16px; width: 100%; margin: 10px 0; }
|
||||
button:hover { background: #218838; }
|
||||
button.cancel { background: #6c757d; }
|
||||
button.cancel:hover { background: #5a6268; }
|
||||
.user { background: #d1ecf1; padding: 8px; margin-bottom: 10px; }
|
||||
.cart-info { background: #f8f9fa; padding: 12px; margin: 15px 0; border-left: 4px solid #007bff; }
|
||||
.info { color: #666; font-size: 0.9em; margin-top: 20px; }
|
||||
.warning { background: #fff3cd; color: #856404; padding: 12px; margin: 15px 0; border-left: 4px solid #ffc107; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Confirm Payment</h1>
|
||||
<div class="user">✓ Logged in as: <strong>${userSession.name}</strong></div>
|
||||
${cartId ? `<div class="cart-info"><strong>Cart ID:</strong> ${cartId}</div>` : ''}
|
||||
<div class="warning">
|
||||
<strong>⚠️ Please review your order before confirming.</strong>
|
||||
</div>
|
||||
<form method="POST" action="/confirm-payment">
|
||||
<input type="hidden" name="session" value="${mcpSessionId}" />
|
||||
<input type="hidden" name="elicitation" value="${elicitationId}" />
|
||||
${cartId ? `<input type="hidden" name="cartId" value="${cartId}" />` : ''}
|
||||
<button type="submit" name="action" value="confirm">Confirm Payment</button>
|
||||
<button type="submit" name="action" value="cancel" class="cancel">Cancel</button>
|
||||
</form>
|
||||
<div class="info">This is a demo showing how a server can securely get user confirmation for sensitive operations using URL-mode elicitation.</div>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
});
|
||||
// Handle Payment Confirmation form submission
|
||||
app.post('/confirm-payment', express.urlencoded(), (req, res) => {
|
||||
const { session: sessionId, elicitation: elicitationId, cartId, action } = req.body;
|
||||
if (!sessionId || !elicitationId) {
|
||||
res.status(400).send('<h1>Error</h1><p>Missing required parameters</p>');
|
||||
return;
|
||||
}
|
||||
// Check for user session cookie here too
|
||||
const userSession = getUserSessionCookie(req.headers.cookie);
|
||||
if (!userSession) {
|
||||
res.status(401).send('<h1>Error</h1><p>Unauthorized - please reconnect to login again</p>');
|
||||
return;
|
||||
}
|
||||
if (action === 'confirm') {
|
||||
// A real app would process the payment here
|
||||
console.log(`💳 Payment confirmed for cart ${cartId || 'unknown'} by user ${userSession.name} (session ${sessionId})`);
|
||||
// Complete the elicitation
|
||||
completeURLElicitation(elicitationId);
|
||||
// Send a success response
|
||||
res.send(`
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Payment Confirmed</title>
|
||||
<style>
|
||||
body { font-family: sans-serif; max-width: 400px; margin: 50px auto; padding: 20px; text-align: center; }
|
||||
.success { background: #d4edda; color: #155724; padding: 20px; margin: 20px 0; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="success">
|
||||
<h1>Payment Confirmed ✓</h1>
|
||||
<p>Your payment has been successfully processed.</p>
|
||||
${cartId ? `<p><strong>Cart ID:</strong> ${cartId}</p>` : ''}
|
||||
</div>
|
||||
<p>You can close this window and return to your MCP client.</p>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
}
|
||||
else if (action === 'cancel') {
|
||||
console.log(`💳 Payment cancelled for cart ${cartId || 'unknown'} by user ${userSession.name} (session ${sessionId})`);
|
||||
// The client will still receive a notifications/elicitation/complete notification,
|
||||
// which indicates that the out-of-band interaction is complete (but not necessarily successful)
|
||||
completeURLElicitation(elicitationId);
|
||||
res.send(`
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Payment Cancelled</title>
|
||||
<style>
|
||||
body { font-family: sans-serif; max-width: 400px; margin: 50px auto; padding: 20px; text-align: center; }
|
||||
.info { background: #d1ecf1; color: #0c5460; padding: 20px; margin: 20px 0; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="info">
|
||||
<h1>Payment Cancelled</h1>
|
||||
<p>Your payment has been cancelled.</p>
|
||||
</div>
|
||||
<p>You can close this window and return to your MCP client.</p>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
}
|
||||
else {
|
||||
res.status(400).send('<h1>Error</h1><p>Invalid action</p>');
|
||||
}
|
||||
});
|
||||
// Map to store transports by session ID
|
||||
const transports = {};
|
||||
const sessionsNeedingElicitation = {};
|
||||
// MCP POST endpoint
|
||||
const mcpPostHandler = async (req, res) => {
|
||||
const sessionId = req.headers['mcp-session-id'];
|
||||
console.debug(`Received MCP POST for session: ${sessionId || 'unknown'}`);
|
||||
try {
|
||||
let transport;
|
||||
if (sessionId && transports[sessionId]) {
|
||||
// Reuse existing transport
|
||||
transport = transports[sessionId];
|
||||
}
|
||||
else if (!sessionId && isInitializeRequest(req.body)) {
|
||||
const server = getServer();
|
||||
// New initialization request
|
||||
const eventStore = new InMemoryEventStore();
|
||||
transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: () => randomUUID(),
|
||||
eventStore, // Enable resumability
|
||||
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;
|
||||
sessionsNeedingElicitation[sessionId] = {
|
||||
elicitationSender: params => server.server.elicitInput(params),
|
||||
createCompletionNotifier: elicitationId => server.server.createElicitationCompletionNotifier(elicitationId)
|
||||
};
|
||||
}
|
||||
});
|
||||
// Set up onclose handler to clean up transport when closed
|
||||
transport.onclose = () => {
|
||||
const sid = transport.sessionId;
|
||||
if (sid && transports[sid]) {
|
||||
console.log(`Transport closed for session ${sid}, removing from transports map`);
|
||||
delete transports[sid];
|
||||
delete sessionsNeedingElicitation[sid];
|
||||
}
|
||||
};
|
||||
// Connect the transport to the MCP server BEFORE handling the request
|
||||
// so responses can flow back through the same transport
|
||||
await server.connect(transport);
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
return; // Already handled
|
||||
}
|
||||
else {
|
||||
// Invalid request - no session ID or not initialization request
|
||||
res.status(400).json({
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32000,
|
||||
message: 'Bad Request: No valid session ID provided'
|
||||
},
|
||||
id: null
|
||||
});
|
||||
return;
|
||||
}
|
||||
// Handle the request with existing transport - no need to reconnect
|
||||
// The existing transport is already connected to the server
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error handling MCP request:', error);
|
||||
if (!res.headersSent) {
|
||||
res.status(500).json({
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32603,
|
||||
message: 'Internal server error'
|
||||
},
|
||||
id: null
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
// Set up routes with auth middleware
|
||||
app.post('/mcp', authMiddleware, mcpPostHandler);
|
||||
// Handle GET requests for SSE streams (using built-in support from StreamableHTTP)
|
||||
const mcpGetHandler = async (req, res) => {
|
||||
const sessionId = req.headers['mcp-session-id'];
|
||||
if (!sessionId || !transports[sessionId]) {
|
||||
res.status(400).send('Invalid or missing session ID');
|
||||
return;
|
||||
}
|
||||
// Check for Last-Event-ID header for resumability
|
||||
const lastEventId = req.headers['last-event-id'];
|
||||
if (lastEventId) {
|
||||
console.log(`Client reconnecting with Last-Event-ID: ${lastEventId}`);
|
||||
}
|
||||
else {
|
||||
console.log(`Establishing new SSE stream for session ${sessionId}`);
|
||||
}
|
||||
const transport = transports[sessionId];
|
||||
await transport.handleRequest(req, res);
|
||||
if (sessionsNeedingElicitation[sessionId]) {
|
||||
const { elicitationSender, createCompletionNotifier } = sessionsNeedingElicitation[sessionId];
|
||||
// Send an elicitation request to the client in the background
|
||||
sendApiKeyElicitation(sessionId, elicitationSender, createCompletionNotifier)
|
||||
.then(() => {
|
||||
// Only delete on successful send for this demo
|
||||
delete sessionsNeedingElicitation[sessionId];
|
||||
console.log(`🔑 URL elicitation demo: Finished sending API key elicitation request for session ${sessionId}`);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error sending API key elicitation:', error);
|
||||
// Keep in map to potentially retry on next reconnect
|
||||
});
|
||||
}
|
||||
};
|
||||
// Set up GET route with conditional auth middleware
|
||||
app.get('/mcp', authMiddleware, mcpGetHandler);
|
||||
// Handle DELETE requests for session termination (according to MCP spec)
|
||||
const mcpDeleteHandler = async (req, res) => {
|
||||
const sessionId = req.headers['mcp-session-id'];
|
||||
if (!sessionId || !transports[sessionId]) {
|
||||
res.status(400).send('Invalid or missing session ID');
|
||||
return;
|
||||
}
|
||||
console.log(`Received session termination request for session ${sessionId}`);
|
||||
try {
|
||||
const transport = transports[sessionId];
|
||||
await transport.handleRequest(req, res);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error handling session termination:', error);
|
||||
if (!res.headersSent) {
|
||||
res.status(500).send('Error processing session termination');
|
||||
}
|
||||
}
|
||||
};
|
||||
// Set up DELETE route with auth middleware
|
||||
app.delete('/mcp', authMiddleware, mcpDeleteHandler);
|
||||
app.listen(MCP_PORT, error => {
|
||||
if (error) {
|
||||
console.error('Failed to start server:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(`MCP Streamable HTTP Server listening on port ${MCP_PORT}`);
|
||||
});
|
||||
// Handle server shutdown
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('Shutting down server...');
|
||||
// Close all active transports to properly clean up resources
|
||||
for (const sessionId in transports) {
|
||||
try {
|
||||
console.log(`Closing transport for session ${sessionId}`);
|
||||
await transports[sessionId].close();
|
||||
delete transports[sessionId];
|
||||
delete sessionsNeedingElicitation[sessionId];
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`Error closing transport for session ${sessionId}:`, error);
|
||||
}
|
||||
}
|
||||
console.log('Server shutdown complete');
|
||||
process.exit(0);
|
||||
});
|
||||
//# sourceMappingURL=elicitationUrlExample.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/elicitationUrlExample.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/elicitationUrlExample.js.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
10
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/honoWebStandardStreamableHttp.d.ts
generated
vendored
Normal file
10
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/honoWebStandardStreamableHttp.d.ts
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* Example MCP server using Hono with WebStandardStreamableHTTPServerTransport
|
||||
*
|
||||
* This example demonstrates using the Web Standard transport directly with Hono,
|
||||
* which works on any runtime: Node.js, Cloudflare Workers, Deno, Bun, etc.
|
||||
*
|
||||
* Run with: npx tsx src/examples/server/honoWebStandardStreamableHttp.ts
|
||||
*/
|
||||
export {};
|
||||
//# sourceMappingURL=honoWebStandardStreamableHttp.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/honoWebStandardStreamableHttp.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/honoWebStandardStreamableHttp.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"honoWebStandardStreamableHttp.d.ts","sourceRoot":"","sources":["../../../../src/examples/server/honoWebStandardStreamableHttp.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG"}
|
||||
60
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/honoWebStandardStreamableHttp.js
generated
vendored
Normal file
60
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/honoWebStandardStreamableHttp.js
generated
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* Example MCP server using Hono with WebStandardStreamableHTTPServerTransport
|
||||
*
|
||||
* This example demonstrates using the Web Standard transport directly with Hono,
|
||||
* which works on any runtime: Node.js, Cloudflare Workers, Deno, Bun, etc.
|
||||
*
|
||||
* Run with: npx tsx src/examples/server/honoWebStandardStreamableHttp.ts
|
||||
*/
|
||||
import { Hono } from 'hono';
|
||||
import { cors } from 'hono/cors';
|
||||
import { serve } from '@hono/node-server';
|
||||
import * as z from 'zod/v4';
|
||||
import { McpServer } from '../../server/mcp.js';
|
||||
import { WebStandardStreamableHTTPServerTransport } from '../../server/webStandardStreamableHttp.js';
|
||||
// Factory function to create a new MCP server per request (stateless mode)
|
||||
const getServer = () => {
|
||||
const server = new McpServer({
|
||||
name: 'hono-webstandard-mcp-server',
|
||||
version: '1.0.0'
|
||||
});
|
||||
// Register a simple greeting tool
|
||||
server.registerTool('greet', {
|
||||
title: 'Greeting Tool',
|
||||
description: 'A simple greeting tool',
|
||||
inputSchema: { name: z.string().describe('Name to greet') }
|
||||
}, async ({ name }) => {
|
||||
return {
|
||||
content: [{ type: 'text', text: `Hello, ${name}! (from Hono + WebStandard transport)` }]
|
||||
};
|
||||
});
|
||||
return server;
|
||||
};
|
||||
// Create the Hono app
|
||||
const app = new Hono();
|
||||
// Enable CORS for all origins
|
||||
app.use('*', cors({
|
||||
origin: '*',
|
||||
allowMethods: ['GET', 'POST', 'DELETE', 'OPTIONS'],
|
||||
allowHeaders: ['Content-Type', 'mcp-session-id', 'Last-Event-ID', 'mcp-protocol-version'],
|
||||
exposeHeaders: ['mcp-session-id', 'mcp-protocol-version']
|
||||
}));
|
||||
// Health check endpoint
|
||||
app.get('/health', c => c.json({ status: 'ok' }));
|
||||
// MCP endpoint - create a fresh transport and server per request (stateless)
|
||||
app.all('/mcp', async (c) => {
|
||||
const transport = new WebStandardStreamableHTTPServerTransport();
|
||||
const server = getServer();
|
||||
await server.connect(transport);
|
||||
return transport.handleRequest(c.req.raw);
|
||||
});
|
||||
// Start the server
|
||||
const PORT = process.env.MCP_PORT ? parseInt(process.env.MCP_PORT, 10) : 3000;
|
||||
console.log(`Starting Hono MCP server on port ${PORT}`);
|
||||
console.log(`Health check: http://localhost:${PORT}/health`);
|
||||
console.log(`MCP endpoint: http://localhost:${PORT}/mcp`);
|
||||
serve({
|
||||
fetch: app.fetch,
|
||||
port: PORT
|
||||
});
|
||||
//# sourceMappingURL=honoWebStandardStreamableHttp.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/honoWebStandardStreamableHttp.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/honoWebStandardStreamableHttp.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"honoWebStandardStreamableHttp.js","sourceRoot":"","sources":["../../../../src/examples/server/honoWebStandardStreamableHttp.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC1C,OAAO,KAAK,CAAC,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,wCAAwC,EAAE,MAAM,2CAA2C,CAAC;AAGrG,2EAA2E;AAC3E,MAAM,SAAS,GAAG,GAAG,EAAE;IACnB,MAAM,MAAM,GAAG,IAAI,SAAS,CAAC;QACzB,IAAI,EAAE,6BAA6B;QACnC,OAAO,EAAE,OAAO;KACnB,CAAC,CAAC;IAEH,kCAAkC;IAClC,MAAM,CAAC,YAAY,CACf,OA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,CAAC,CAAC;AAC9C,CAAC,CAAC,CAAC;AAEH,mBAAmB;AACnB,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AAE9E,OAAO,CAAC,GAAG,CAAC,oCAAoC,IAAI,EAAE,CAAC,CAAC;AACxD,OAAO,CAAC,GAAG,CAAC,kCAAkC,IAAI,SAAS,CAAC,CAAC;AAC7D,OAAO,CAAC,GAAG,CAAC,kCAAkC,IAAI,MAAM,CAAC,CAAC;AAE1D,KAAK,CAAC;IACF,KAAK,EAAE,GAAG,CAAC,KAAK;IAChB,IAAI,EAAE,IAAI;CACb,CAAC,CAAC"}
|
||||
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/jsonResponseStreamableHttp.d.ts
generated
vendored
Normal file
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/jsonResponseStreamableHttp.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=jsonResponseStreamableHttp.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/jsonResponseStreamableHttp.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/jsonResponseStreamableHttp.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"jsonResponseStreamableHttp.d.ts","sourceRoot":"","sources":["../../../../src/examples/server/jsonResponseStreamableHttp.ts"],"names":[],"mappings":""}
|
||||
146
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/jsonResponseStreamableHttp.js
generated
vendored
Normal file
146
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/jsonResponseStreamableHttp.js
generated
vendored
Normal file
@@ -0,0 +1,146 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { McpServer } from '../../server/mcp.js';
|
||||
import { StreamableHTTPServerTransport } from '../../server/streamableHttp.js';
|
||||
import * as z from 'zod/v4';
|
||||
import { isInitializeRequest } from '../../types.js';
|
||||
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'
|
||||
}, {
|
||||
capabilities: {
|
||||
logging: {}
|
||||
}
|
||||
});
|
||||
// Register a simple tool that returns a greeting
|
||||
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}!`
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
// Register a tool that sends multiple greetings with notifications
|
||||
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 server.sendLoggingMessage({
|
||||
level: 'debug',
|
||||
data: `Starting multi-greet for ${name}`
|
||||
}, extra.sessionId);
|
||||
await sleep(1000); // Wait 1 second before first greeting
|
||||
await server.sendLoggingMessage({
|
||||
level: 'info',
|
||||
data: `Sending first greeting to ${name}`
|
||||
}, extra.sessionId);
|
||||
await sleep(1000); // Wait another second before second greeting
|
||||
await server.sendLoggingMessage({
|
||||
level: 'info',
|
||||
data: `Sending second greeting to ${name}`
|
||||
}, extra.sessionId);
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Good morning, ${name}!`
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
return server;
|
||||
};
|
||||
const app = createMcpExpressApp();
|
||||
// Map to store transports by session ID
|
||||
const transports = {};
|
||||
app.post('/mcp', async (req, res) => {
|
||||
console.log('Received MCP request:', req.body);
|
||||
try {
|
||||
// Check for existing session ID
|
||||
const sessionId = req.headers['mcp-session-id'];
|
||||
let transport;
|
||||
if (sessionId && transports[sessionId]) {
|
||||
// Reuse existing transport
|
||||
transport = transports[sessionId];
|
||||
}
|
||||
else if (!sessionId && isInitializeRequest(req.body)) {
|
||||
// New initialization request - use JSON response mode
|
||||
transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: () => randomUUID(),
|
||||
enableJsonResponse: true, // Enable JSON response mode
|
||||
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 BEFORE handling the request
|
||||
const server = getServer();
|
||||
await server.connect(transport);
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
return; // Already handled
|
||||
}
|
||||
else {
|
||||
// Invalid request - no session ID or not initialization request
|
||||
res.status(400).json({
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32000,
|
||||
message: 'Bad Request: No valid session ID provided'
|
||||
},
|
||||
id: null
|
||||
});
|
||||
return;
|
||||
}
|
||||
// Handle the request with existing transport - no need to reconnect
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error handling MCP request:', error);
|
||||
if (!res.headersSent) {
|
||||
res.status(500).json({
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32603,
|
||||
message: 'Internal server error'
|
||||
},
|
||||
id: null
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
// Handle GET requests for SSE streams according to spec
|
||||
app.get('/mcp', async (req, res) => {
|
||||
// Since this is a very simple example, we don't support GET requests for this server
|
||||
// The spec requires returning 405 Method Not Allowed in this case
|
||||
res.status(405).set('Allow', 'POST').send('Method Not Allowed');
|
||||
});
|
||||
// Start the server
|
||||
const PORT = 3000;
|
||||
app.listen(PORT, error => {
|
||||
if (error) {
|
||||
console.error('Failed to start server:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(`MCP Streamable HTTP Server listening on port ${PORT}`);
|
||||
});
|
||||
// Handle server shutdown
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('Shutting down server...');
|
||||
process.exit(0);
|
||||
});
|
||||
//# sourceMappingURL=jsonResponseStreamableHttp.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/jsonResponseStreamableHttp.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/jsonResponseStreamableHttp.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"jsonResponseStreamableHttp.js","sourceRoot":"","sources":["../../../../src/examples/server/jsonResponseStreamableHttp.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,KAAK,CAAC,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAkB,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACrE,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAE9D,mDAAmD;AACnD,MAAM,SAAS,GAAG,GAAG,EAAE;IACnB,MAAM,MAAM,GAAG,IAAI,SAAS,CACxB;QACI,IAAI,EAAE,sCAAsC;QAC5C,OAAO,EAAE,OAAO;KACnB,EACD;QACI,YAAY,EAAE;YACV,OAAO,EAAE,EAAE;SACd;KACJ,CACJ,CAAC;IAEF,iDAAiD;IACjD,MAAM,CAAC,YAAY,CACf,OAAO,EACP;QACI,WAAW,EAAE,wBAAwB;QACrC,WAAW,EAAE;YACT,IAAI,E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G,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"}
|
||||
7
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/mcpServerOutputSchema.d.ts
generated
vendored
Normal file
7
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/mcpServerOutputSchema.d.ts
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Example MCP server using the high-level McpServer API with outputSchema
|
||||
* This demonstrates how to easily create tools with structured output
|
||||
*/
|
||||
export {};
|
||||
//# sourceMappingURL=mcpServerOutputSchema.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/mcpServerOutputSchema.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/mcpServerOutputSchema.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"mcpServerOutputSchema.d.ts","sourceRoot":"","sources":["../../../../src/examples/server/mcpServerOutputSchema.ts"],"names":[],"mappings":";AACA;;;GAGG"}
|
||||
70
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/mcpServerOutputSchema.js
generated
vendored
Normal file
70
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/mcpServerOutputSchema.js
generated
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* 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 * as z from 'zod/v4';
|
||||
const server = new McpServer({
|
||||
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',
|
||||
inputSchema: {
|
||||
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']),
|
||||
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 structuredContent = {
|
||||
temperature: {
|
||||
celsius: temp_c,
|
||||
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)]
|
||||
}
|
||||
};
|
||||
return {
|
||||
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');
|
||||
}
|
||||
main().catch(error => {
|
||||
console.error('Server error:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
//# sourceMappingURL=mcpServerOutputSchema.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/mcpServerOutputSchema.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/mcpServerOutputSchema.js.map
generated
vendored
Normal file
@@ -0,0 +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,KAAK,CAAC,MAAM,QAAQ,CAAC;AAE5B,MAAM,MAAM,GAAG,IAAI,SAAS,CAAC;IACzB,IAAI,EAAE,sCAAsC;IAC5C,OAAO,EAAE,OAAO;CACnB,CAAC,CAAC;AAEH,sDAAsD;AACtD,MAAM,CAAC,YAAY,CACf,aAAa,EACb;IACI,WAAW,EAAE,oCAAoC;IACjD,WAAW,EAAE;QACT,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,WAAW,CAAC;QACtC,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,6BAA6B,CAAC;KAC9D;IACD,YAAY,EAAE;QACV,WAAW,EAAE,CAAC,CAAC,MAAM,CAAC;YAClB,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE;YACnB,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE;SACzB,CAAC;QACF,UAAU,E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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;SACzF;KACJ,CAAC;IAEF,OAAO;QACH,OAAO,EAAE;YACL;gBACI,IAAI,EAAE,MAAM;gBACZ,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,iBAAiB,EAAE,IAAI,EAAE,CAAC,CAAC;aACnD;SACJ;QACD,iBAAiB;KACpB,CAAC;AACN,CAAC,CACJ,CAAC;AAEF,KAAK,UAAU,IAAI;IACf,MAAM,SAAS,GAAG,IAAI,oBAAoB,EAAE,CAAC;IAC7C,MAAM,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IAChC,OAAO,CAAC,KAAK,CAAC,0DAA0D,CAAC,CAAC;AAC9E,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"}
|
||||
12
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/progressExample.d.ts
generated
vendored
Normal file
12
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/progressExample.d.ts
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Example: Progress notifications over stdio.
|
||||
*
|
||||
* Demonstrates a tool that reports progress to the client while processing.
|
||||
*
|
||||
* Run:
|
||||
* npx tsx src/examples/server/progressExample.ts
|
||||
*
|
||||
* Then connect a client with an `onprogress` callback (see docs/protocol.md).
|
||||
*/
|
||||
export {};
|
||||
//# sourceMappingURL=progressExample.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/progressExample.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/progressExample.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"progressExample.d.ts","sourceRoot":"","sources":["../../../../src/examples/server/progressExample.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG"}
|
||||
47
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/progressExample.js
generated
vendored
Normal file
47
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/progressExample.js
generated
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Example: Progress notifications over stdio.
|
||||
*
|
||||
* Demonstrates a tool that reports progress to the client while processing.
|
||||
*
|
||||
* Run:
|
||||
* npx tsx src/examples/server/progressExample.ts
|
||||
*
|
||||
* Then connect a client with an `onprogress` callback (see docs/protocol.md).
|
||||
*/
|
||||
import { McpServer } from '../../server/mcp.js';
|
||||
import { StdioServerTransport } from '../../server/stdio.js';
|
||||
import { z } from 'zod';
|
||||
const server = new McpServer({ name: 'progress-example', version: '1.0.0' }, { capabilities: { logging: {} } });
|
||||
server.registerTool('count', {
|
||||
description: 'Count to N with progress updates',
|
||||
inputSchema: { n: z.number().int().min(1).max(100) }
|
||||
}, async ({ n }, extra) => {
|
||||
for (let i = 1; i <= n; i++) {
|
||||
if (extra.signal.aborted) {
|
||||
return { content: [{ type: 'text', text: `Cancelled at ${i}` }], isError: true };
|
||||
}
|
||||
if (extra._meta?.progressToken !== undefined) {
|
||||
await extra.sendNotification({
|
||||
method: 'notifications/progress',
|
||||
params: {
|
||||
progressToken: extra._meta.progressToken,
|
||||
progress: i,
|
||||
total: n,
|
||||
message: `Counting: ${i}/${n}`
|
||||
}
|
||||
});
|
||||
}
|
||||
// Simulate work
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
}
|
||||
return { content: [{ type: 'text', text: `Counted to ${n}` }] };
|
||||
});
|
||||
async function main() {
|
||||
const transport = new StdioServerTransport();
|
||||
await server.connect(transport);
|
||||
}
|
||||
main().catch(error => {
|
||||
console.error('Server error:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
//# sourceMappingURL=progressExample.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/progressExample.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/progressExample.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"progressExample.js","sourceRoot":"","sources":["../../../../src/examples/server/progressExample.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;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,CAAC,EAAE,IAAI,EAAE,kBAAkB,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,YAAY,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;AAEhH,MAAM,CAAC,YAAY,CACf,OAAO,EACP;IACI,WAAW,EAAE,kCAAkC;IAC/C,WAAW,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;CACvD,EACD,KAAK,EAAE,EAAE,CAAC,EAAE,EAAE,KAAK,EAAE,EAAE;IACnB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC1B,IAAI,KAAK,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;YACvB,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,gBAAgB,CAAC,EAAE,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;QACrF,CAAC;QAED,IAAI,KAAK,CAAC,KAAK,EAAE,aAAa,KAAK,SAAS,EAAE,CAAC;YAC3C,MAAM,KAAK,CAAC,gBAAgB,CAAC;gBACzB,MAAM,EAAE,wBAAwB;gBAChC,MAAM,EAAE;oBACJ,aAAa,EAAE,KAAK,CAAC,KAAK,CAAC,aAAa;oBACxC,QAAQ,EAAE,CAAC;oBACX,KAAK,EAAE,CAAC;oBACR,OAAO,EAAE,aAAa,CAAC,IAAI,CAAC,EAAE;iBACjC;aACJ,CAAC,CAAC;QACP,CAAC;QAED,gBAAgB;QAChB,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC;IAC3D,CAAC;IAED,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,cAAc,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC;AACpE,CAAC,CACJ,CAAC;AAEF,KAAK,UAAU,IAAI;IACf,MAAM,SAAS,GAAG,IAAI,oBAAoB,EAAE,CAAC;IAC7C,MAAM,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;AACpC,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"}
|
||||
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleSseServer.d.ts
generated
vendored
Normal file
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleSseServer.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=simpleSseServer.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleSseServer.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleSseServer.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"simpleSseServer.d.ts","sourceRoot":"","sources":["../../../../src/examples/server/simpleSseServer.ts"],"names":[],"mappings":""}
|
||||
143
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleSseServer.js
generated
vendored
Normal file
143
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleSseServer.js
generated
vendored
Normal file
@@ -0,0 +1,143 @@
|
||||
import { McpServer } from '../../server/mcp.js';
|
||||
import { SSEServerTransport } from '../../server/sse.js';
|
||||
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.
|
||||
*
|
||||
* The server exposes two endpoints:
|
||||
* - /mcp: For establishing the SSE stream (GET)
|
||||
* - /messages: For receiving client messages (POST)
|
||||
*
|
||||
*/
|
||||
// Create an MCP server instance
|
||||
const getServer = () => {
|
||||
const server = new McpServer({
|
||||
name: 'simple-sse-server',
|
||||
version: '1.0.0'
|
||||
}, { capabilities: { logging: {} } });
|
||||
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 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 server.sendLoggingMessage({
|
||||
level: 'info',
|
||||
data: `Notification #${counter} at ${new Date().toISOString()}`
|
||||
}, extra.sessionId);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error sending notification:', error);
|
||||
}
|
||||
}
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Completed sending ${count} notifications every ${interval}ms`
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
return server;
|
||||
};
|
||||
const app = createMcpExpressApp();
|
||||
// Store transports by session ID
|
||||
const transports = {};
|
||||
// SSE endpoint for establishing the stream
|
||||
app.get('/mcp', async (req, res) => {
|
||||
console.log('Received GET request to /sse (establishing SSE stream)');
|
||||
try {
|
||||
// Create a new SSE transport for the client
|
||||
// The endpoint for POST messages is '/messages'
|
||||
const transport = new SSEServerTransport('/messages', res);
|
||||
// Store the transport by session ID
|
||||
const sessionId = transport.sessionId;
|
||||
transports[sessionId] = transport;
|
||||
// Set up onclose handler to clean up transport when closed
|
||||
transport.onclose = () => {
|
||||
console.log(`SSE transport closed for session ${sessionId}`);
|
||||
delete transports[sessionId];
|
||||
};
|
||||
// Connect the transport to the MCP server
|
||||
const server = getServer();
|
||||
await server.connect(transport);
|
||||
console.log(`Established SSE stream with session ID: ${sessionId}`);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error establishing SSE stream:', error);
|
||||
if (!res.headersSent) {
|
||||
res.status(500).send('Error establishing SSE stream');
|
||||
}
|
||||
}
|
||||
});
|
||||
// Messages endpoint for receiving client JSON-RPC requests
|
||||
app.post('/messages', async (req, res) => {
|
||||
console.log('Received POST request to /messages');
|
||||
// Extract session ID from URL query parameter
|
||||
// In the SSE protocol, this is added by the client based on the endpoint event
|
||||
const sessionId = req.query.sessionId;
|
||||
if (!sessionId) {
|
||||
console.error('No session ID provided in request URL');
|
||||
res.status(400).send('Missing sessionId parameter');
|
||||
return;
|
||||
}
|
||||
const transport = transports[sessionId];
|
||||
if (!transport) {
|
||||
console.error(`No active transport found for session ID: ${sessionId}`);
|
||||
res.status(404).send('Session not found');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// Handle the POST message with the transport
|
||||
await transport.handlePostMessage(req, res, req.body);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error handling request:', error);
|
||||
if (!res.headersSent) {
|
||||
res.status(500).send('Error handling request');
|
||||
}
|
||||
}
|
||||
});
|
||||
// Start the server
|
||||
const PORT = 3000;
|
||||
app.listen(PORT, error => {
|
||||
if (error) {
|
||||
console.error('Failed to start server:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(`Simple SSE Server (deprecated protocol version 2024-11-05) listening on port ${PORT}`);
|
||||
});
|
||||
// Handle server shutdown
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('Shutting down server...');
|
||||
// Close all active transports to properly clean up resources
|
||||
for (const sessionId in transports) {
|
||||
try {
|
||||
console.log(`Closing transport for session ${sessionId}`);
|
||||
await transports[sessionId].close();
|
||||
delete transports[sessionId];
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`Error closing transport for session ${sessionId}:`, error);
|
||||
}
|
||||
}
|
||||
console.log('Server shutdown complete');
|
||||
process.exit(0);
|
||||
});
|
||||
//# sourceMappingURL=simpleSseServer.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleSseServer.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleSseServer.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"simpleSseServer.js","sourceRoot":"","sources":["../../../../src/examples/server/simpleSseServer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,KAAK,CAAC,MAAM,QAAQ,CAAC;AAE5B,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;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modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleStatelessStreamableHttp.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=simpleStatelessStreamableHttp.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleStatelessStreamableHttp.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleStatelessStreamableHttp.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"simpleStatelessStreamableHttp.d.ts","sourceRoot":"","sources":["../../../../src/examples/server/simpleStatelessStreamableHttp.ts"],"names":[],"mappings":""}
|
||||
141
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleStatelessStreamableHttp.js
generated
vendored
Normal file
141
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleStatelessStreamableHttp.js
generated
vendored
Normal file
@@ -0,0 +1,141 @@
|
||||
import { McpServer } from '../../server/mcp.js';
|
||||
import { StreamableHTTPServerTransport } from '../../server/streamableHttp.js';
|
||||
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'
|
||||
}, { capabilities: { logging: {} } });
|
||||
// Register a simple prompt
|
||||
server.registerPrompt('greeting-template', {
|
||||
description: 'A simple greeting prompt template',
|
||||
argsSchema: {
|
||||
name: z.string().describe('Name to include in greeting')
|
||||
}
|
||||
}, async ({ name }) => {
|
||||
return {
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: {
|
||||
type: 'text',
|
||||
text: `Please greet ${name} in a friendly manner.`
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
// Register a tool specifically for testing resumability
|
||||
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 server.sendLoggingMessage({
|
||||
level: 'info',
|
||||
data: `Periodic notification #${counter} at ${new Date().toISOString()}`
|
||||
}, extra.sessionId);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error sending notification:', error);
|
||||
}
|
||||
// Wait for the specified interval
|
||||
await sleep(interval);
|
||||
}
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Started sending periodic notifications every ${interval}ms`
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
// Create a simple resource at a fixed URI
|
||||
server.registerResource('greeting-resource', 'https://example.com/greetings/default', { mimeType: 'text/plain' }, async () => {
|
||||
return {
|
||||
contents: [
|
||||
{
|
||||
uri: 'https://example.com/greetings/default',
|
||||
text: 'Hello, world!'
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
return server;
|
||||
};
|
||||
const app = createMcpExpressApp();
|
||||
app.post('/mcp', async (req, res) => {
|
||||
const server = getServer();
|
||||
try {
|
||||
const transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: undefined
|
||||
});
|
||||
await server.connect(transport);
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
res.on('close', () => {
|
||||
console.log('Request closed');
|
||||
transport.close();
|
||||
server.close();
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error handling MCP request:', error);
|
||||
if (!res.headersSent) {
|
||||
res.status(500).json({
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32603,
|
||||
message: 'Internal server error'
|
||||
},
|
||||
id: null
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
app.get('/mcp', async (req, res) => {
|
||||
console.log('Received GET MCP request');
|
||||
res.writeHead(405).end(JSON.stringify({
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32000,
|
||||
message: 'Method not allowed.'
|
||||
},
|
||||
id: null
|
||||
}));
|
||||
});
|
||||
app.delete('/mcp', async (req, res) => {
|
||||
console.log('Received DELETE MCP request');
|
||||
res.writeHead(405).end(JSON.stringify({
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32000,
|
||||
message: 'Method not allowed.'
|
||||
},
|
||||
id: null
|
||||
}));
|
||||
});
|
||||
// Start the server
|
||||
const PORT = 3000;
|
||||
app.listen(PORT, error => {
|
||||
if (error) {
|
||||
console.error('Failed to start server:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(`MCP Stateless Streamable HTTP Server listening on port ${PORT}`);
|
||||
});
|
||||
// Handle server shutdown
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('Shutting down server...');
|
||||
process.exit(0);
|
||||
});
|
||||
//# sourceMappingURL=simpleStatelessStreamableHttp.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleStatelessStreamableHttp.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleStatelessStreamableHttp.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"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,M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C,SAAS,CAAC,GAAG,CAAC,CAAC,GAAG,CAClB,IAAI,CAAC,SAAS,CAAC;QACX,OAAO,EAAE,KAAK;QACd,KAAK,EAAE;YACH,IAAI,EAAE,CAAC,KAAK;YACZ,OAAO,EAAE,qBAAqB;SACjC;QACD,EAAE,EAAE,IAAI;KACX,CAAC,CACL,CAAC;AACN,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,0DAA0D,IAAI,EAAE,CAAC,CAAC;AAClF,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"}
|
||||
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleStreamableHttp.d.ts
generated
vendored
Normal file
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleStreamableHttp.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=simpleStreamableHttp.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleStreamableHttp.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleStreamableHttp.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"simpleStreamableHttp.d.ts","sourceRoot":"","sources":["../../../../src/examples/server/simpleStreamableHttp.ts"],"names":[],"mappings":""}
|
||||
725
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleStreamableHttp.js
generated
vendored
Normal file
725
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleStreamableHttp.js
generated
vendored
Normal file
@@ -0,0 +1,725 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
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 { 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 '../../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',
|
||||
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')
|
||||
}
|
||||
}, async ({ name }) => {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Hello, ${name}!`
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
// Register a tool that sends multiple greetings with notifications (with annotations)
|
||||
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 server.sendLoggingMessage({
|
||||
level: 'debug',
|
||||
data: `Starting multi-greet for ${name}`
|
||||
}, extra.sessionId);
|
||||
await sleep(1000); // Wait 1 second before first greeting
|
||||
await server.sendLoggingMessage({
|
||||
level: 'info',
|
||||
data: `Sending first greeting to ${name}`
|
||||
}, extra.sessionId);
|
||||
await sleep(1000); // Wait another second before second greeting
|
||||
await server.sendLoggingMessage({
|
||||
level: 'info',
|
||||
data: `Sending second greeting to ${name}`
|
||||
}, extra.sessionId);
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Good morning, ${name}!`
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
// Register a tool that demonstrates form elicitation (user input collection with a schema)
|
||||
// This creates a closure that captures the server instance
|
||||
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) {
|
||||
case 'contact':
|
||||
message = 'Please provide your contact information';
|
||||
requestedSchema = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: {
|
||||
type: 'string',
|
||||
title: 'Full Name',
|
||||
description: 'Your full name'
|
||||
},
|
||||
email: {
|
||||
type: 'string',
|
||||
title: 'Email Address',
|
||||
description: 'Your email address',
|
||||
format: 'email'
|
||||
},
|
||||
phone: {
|
||||
type: 'string',
|
||||
title: 'Phone Number',
|
||||
description: 'Your phone number (optional)'
|
||||
}
|
||||
},
|
||||
required: ['name', 'email']
|
||||
};
|
||||
break;
|
||||
case 'preferences':
|
||||
message = 'Please set your preferences';
|
||||
requestedSchema = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
theme: {
|
||||
type: 'string',
|
||||
title: 'Theme',
|
||||
description: 'Choose your preferred theme',
|
||||
enum: ['light', 'dark', 'auto'],
|
||||
enumNames: ['Light', 'Dark', 'Auto']
|
||||
},
|
||||
notifications: {
|
||||
type: 'boolean',
|
||||
title: 'Enable Notifications',
|
||||
description: 'Would you like to receive notifications?',
|
||||
default: true
|
||||
},
|
||||
frequency: {
|
||||
type: 'string',
|
||||
title: 'Notification Frequency',
|
||||
description: 'How often would you like notifications?',
|
||||
enum: ['daily', 'weekly', 'monthly'],
|
||||
enumNames: ['Daily', 'Weekly', 'Monthly']
|
||||
}
|
||||
},
|
||||
required: ['theme']
|
||||
};
|
||||
break;
|
||||
case 'feedback':
|
||||
message = 'Please provide your feedback';
|
||||
requestedSchema = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
rating: {
|
||||
type: 'integer',
|
||||
title: 'Rating',
|
||||
description: 'Rate your experience (1-5)',
|
||||
minimum: 1,
|
||||
maximum: 5
|
||||
},
|
||||
comments: {
|
||||
type: 'string',
|
||||
title: 'Comments',
|
||||
description: 'Additional comments (optional)',
|
||||
maxLength: 500
|
||||
},
|
||||
recommend: {
|
||||
type: 'boolean',
|
||||
title: 'Would you recommend this?',
|
||||
description: 'Would you recommend this to others?'
|
||||
}
|
||||
},
|
||||
required: ['rating', 'recommend']
|
||||
};
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unknown info type: ${infoType}`);
|
||||
}
|
||||
try {
|
||||
// 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)}`
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
else if (result.action === 'decline') {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `No information was collected. User declined ${infoType} information request.`
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
else {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Information collection was cancelled by the user.`
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Error collecting ${infoType} information: ${error}`
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
});
|
||||
// Register a tool that demonstrates bidirectional task support:
|
||||
// Server creates a task, then elicits input from client using elicitInputStream
|
||||
// Using the experimental tasks API - WARNING: may change without notice
|
||||
server.experimental.tasks.registerToolTask('collect-user-info-task', {
|
||||
title: 'Collect Info with Task',
|
||||
description: 'Collects user info via elicitation with task support using elicitInputStream',
|
||||
inputSchema: {
|
||||
infoType: z.enum(['contact', 'preferences']).describe('Type of information to collect').default('contact')
|
||||
}
|
||||
}, {
|
||||
async createTask({ infoType }, { taskStore: createTaskStore, taskRequestedTtl }) {
|
||||
// Create the server-side task
|
||||
const task = await createTaskStore.createTask({
|
||||
ttl: taskRequestedTtl
|
||||
});
|
||||
// Perform async work that makes a nested elicitation request using elicitInputStream
|
||||
(async () => {
|
||||
try {
|
||||
const message = infoType === 'contact' ? 'Please provide your contact information' : 'Please set your preferences';
|
||||
// Define schemas with proper typing for PrimitiveSchemaDefinition
|
||||
const contactSchema = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string', title: 'Full Name', description: 'Your full name' },
|
||||
email: { type: 'string', title: 'Email', description: 'Your email address' }
|
||||
},
|
||||
required: ['name', 'email']
|
||||
};
|
||||
const preferencesSchema = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
theme: { type: 'string', title: 'Theme', enum: ['light', 'dark', 'auto'] },
|
||||
notifications: { type: 'boolean', title: 'Enable Notifications', default: true }
|
||||
},
|
||||
required: ['theme']
|
||||
};
|
||||
const requestedSchema = infoType === 'contact' ? contactSchema : preferencesSchema;
|
||||
// Use elicitInputStream to elicit input from client
|
||||
// This demonstrates the streaming elicitation API
|
||||
// Access via server.server to get the underlying Server instance
|
||||
const stream = server.server.experimental.tasks.elicitInputStream({
|
||||
mode: 'form',
|
||||
message,
|
||||
requestedSchema
|
||||
});
|
||||
let elicitResult;
|
||||
for await (const msg of stream) {
|
||||
if (msg.type === 'result') {
|
||||
elicitResult = msg.result;
|
||||
}
|
||||
else if (msg.type === 'error') {
|
||||
throw msg.error;
|
||||
}
|
||||
}
|
||||
if (!elicitResult) {
|
||||
throw new Error('No result received from elicitation');
|
||||
}
|
||||
let resultText;
|
||||
if (elicitResult.action === 'accept') {
|
||||
resultText = `Collected ${infoType} info: ${JSON.stringify(elicitResult.content, null, 2)}`;
|
||||
}
|
||||
else if (elicitResult.action === 'decline') {
|
||||
resultText = `User declined to provide ${infoType} information`;
|
||||
}
|
||||
else {
|
||||
resultText = 'User cancelled the request';
|
||||
}
|
||||
await taskStore.storeTaskResult(task.taskId, 'completed', {
|
||||
content: [{ type: 'text', text: resultText }]
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error in collect-user-info-task:', error);
|
||||
await taskStore.storeTaskResult(task.taskId, 'failed', {
|
||||
content: [{ type: 'text', text: `Error: ${error}` }],
|
||||
isError: true
|
||||
});
|
||||
}
|
||||
})();
|
||||
return { task };
|
||||
},
|
||||
async getTask(_args, { taskId, taskStore: getTaskStore }) {
|
||||
return await getTaskStore.getTask(taskId);
|
||||
},
|
||||
async getTaskResult(_args, { taskId, taskStore: getResultTaskStore }) {
|
||||
const result = await getResultTaskStore.getTaskResult(taskId);
|
||||
return result;
|
||||
}
|
||||
});
|
||||
// Register a simple prompt with title
|
||||
server.registerPrompt('greeting-template', {
|
||||
title: 'Greeting Template', // Display name for UI
|
||||
description: 'A simple greeting prompt template',
|
||||
argsSchema: {
|
||||
name: z.string().describe('Name to include in greeting')
|
||||
}
|
||||
}, async ({ name }) => {
|
||||
return {
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: {
|
||||
type: 'text',
|
||||
text: `Please greet ${name} in a friendly manner.`
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
// Register a tool specifically for testing resumability
|
||||
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 server.sendLoggingMessage({
|
||||
level: 'info',
|
||||
data: `Periodic notification #${counter} at ${new Date().toISOString()}`
|
||||
}, extra.sessionId);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error sending notification:', error);
|
||||
}
|
||||
// Wait for the specified interval
|
||||
await sleep(interval);
|
||||
}
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Started sending periodic notifications every ${interval}ms`
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
// Create a simple resource at a fixed URI
|
||||
server.registerResource('greeting-resource', 'https://example.com/greetings/default', {
|
||||
title: 'Default Greeting', // Display name for UI
|
||||
description: 'A simple greeting resource',
|
||||
mimeType: 'text/plain'
|
||||
}, async () => {
|
||||
return {
|
||||
contents: [
|
||||
{
|
||||
uri: 'https://example.com/greetings/default',
|
||||
text: 'Hello, world!'
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
// Create additional resources for ResourceLink demonstration
|
||||
server.registerResource('example-file-1', 'file:///example/file1.txt', {
|
||||
title: 'Example File 1',
|
||||
description: 'First example file for ResourceLink demonstration',
|
||||
mimeType: 'text/plain'
|
||||
}, async () => {
|
||||
return {
|
||||
contents: [
|
||||
{
|
||||
uri: 'file:///example/file1.txt',
|
||||
text: 'This is the content of file 1'
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
server.registerResource('example-file-2', 'file:///example/file2.txt', {
|
||||
title: 'Example File 2',
|
||||
description: 'Second example file for ResourceLink demonstration',
|
||||
mimeType: 'text/plain'
|
||||
}, async () => {
|
||||
return {
|
||||
contents: [
|
||||
{
|
||||
uri: 'file:///example/file2.txt',
|
||||
text: 'This is the content of file 2'
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
// Register a tool that returns ResourceLinks
|
||||
server.registerTool('list-files', {
|
||||
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')
|
||||
}
|
||||
}, async ({ includeDescriptions = true }) => {
|
||||
const resourceLinks = [
|
||||
{
|
||||
type: 'resource_link',
|
||||
uri: 'https://example.com/greetings/default',
|
||||
name: 'Default Greeting',
|
||||
mimeType: 'text/plain',
|
||||
...(includeDescriptions && { description: 'A simple greeting resource' })
|
||||
},
|
||||
{
|
||||
type: 'resource_link',
|
||||
uri: 'file:///example/file1.txt',
|
||||
name: 'Example File 1',
|
||||
mimeType: 'text/plain',
|
||||
...(includeDescriptions && { description: 'First example file for ResourceLink demonstration' })
|
||||
},
|
||||
{
|
||||
type: 'resource_link',
|
||||
uri: 'file:///example/file2.txt',
|
||||
name: 'Example File 2',
|
||||
mimeType: 'text/plain',
|
||||
...(includeDescriptions && { description: 'Second example file for ResourceLink demonstration' })
|
||||
}
|
||||
];
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: 'Here are the available files as resource links:'
|
||||
},
|
||||
...resourceLinks,
|
||||
{
|
||||
type: 'text',
|
||||
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 = createMcpExpressApp();
|
||||
// Set up OAuth if enabled
|
||||
let authMiddleware = null;
|
||||
if (useOAuth) {
|
||||
// Create auth middleware for MCP endpoints
|
||||
const mcpServerUrl = new URL(`http://localhost:${MCP_PORT}/mcp`);
|
||||
const authServerUrl = new URL(`http://localhost:${AUTH_PORT}`);
|
||||
const oauthMetadata = setupAuthServer({ authServerUrl, mcpServerUrl, strictResource: strictOAuth });
|
||||
const tokenVerifier = {
|
||||
verifyAccessToken: async (token) => {
|
||||
const endpoint = oauthMetadata.introspection_endpoint;
|
||||
if (!endpoint) {
|
||||
throw new Error('No token verification endpoint available in metadata');
|
||||
}
|
||||
const response = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
token: token
|
||||
}).toString()
|
||||
});
|
||||
if (!response.ok) {
|
||||
const text = await response.text().catch(() => null);
|
||||
throw new Error(`Invalid or expired token: ${text}`);
|
||||
}
|
||||
const data = await response.json();
|
||||
if (strictOAuth) {
|
||||
if (!data.aud) {
|
||||
throw new Error(`Resource Indicator (RFC8707) missing`);
|
||||
}
|
||||
if (!checkResourceAllowed({ requestedResource: data.aud, configuredResource: mcpServerUrl })) {
|
||||
throw new Error(`Expected resource indicator ${mcpServerUrl}, got: ${data.aud}`);
|
||||
}
|
||||
}
|
||||
// Convert the response to AuthInfo format
|
||||
return {
|
||||
token,
|
||||
clientId: data.client_id,
|
||||
scopes: data.scope ? data.scope.split(' ') : [],
|
||||
expiresAt: data.exp
|
||||
};
|
||||
}
|
||||
};
|
||||
// Add metadata routes to the main MCP server
|
||||
app.use(mcpAuthMetadataRouter({
|
||||
oauthMetadata,
|
||||
resourceServerUrl: mcpServerUrl,
|
||||
scopesSupported: ['mcp:tools'],
|
||||
resourceName: 'MCP Demo Server'
|
||||
}));
|
||||
authMiddleware = requireBearerAuth({
|
||||
verifier: tokenVerifier,
|
||||
requiredScopes: [],
|
||||
resourceMetadataUrl: getOAuthProtectedResourceMetadataUrl(mcpServerUrl)
|
||||
});
|
||||
}
|
||||
// Map to store transports by session ID
|
||||
const transports = {};
|
||||
// MCP POST endpoint with optional auth
|
||||
const mcpPostHandler = async (req, res) => {
|
||||
const sessionId = req.headers['mcp-session-id'];
|
||||
if (sessionId) {
|
||||
console.log(`Received MCP request for session: ${sessionId}`);
|
||||
}
|
||||
else {
|
||||
console.log('Request body:', req.body);
|
||||
}
|
||||
if (useOAuth && req.auth) {
|
||||
console.log('Authenticated user:', req.auth);
|
||||
}
|
||||
try {
|
||||
let transport;
|
||||
if (sessionId && transports[sessionId]) {
|
||||
// Reuse existing transport
|
||||
transport = transports[sessionId];
|
||||
}
|
||||
else if (!sessionId && isInitializeRequest(req.body)) {
|
||||
// New initialization request
|
||||
const eventStore = new InMemoryEventStore();
|
||||
transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: () => randomUUID(),
|
||||
eventStore, // Enable resumability
|
||||
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;
|
||||
}
|
||||
});
|
||||
// Set up onclose handler to clean up transport when closed
|
||||
transport.onclose = () => {
|
||||
const sid = transport.sessionId;
|
||||
if (sid && transports[sid]) {
|
||||
console.log(`Transport closed for session ${sid}, removing from transports map`);
|
||||
delete transports[sid];
|
||||
}
|
||||
};
|
||||
// Connect the transport to the MCP server BEFORE handling the request
|
||||
// so responses can flow back through the same transport
|
||||
const server = getServer();
|
||||
await server.connect(transport);
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
return; // Already handled
|
||||
}
|
||||
else {
|
||||
// Invalid request - no session ID or not initialization request
|
||||
res.status(400).json({
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32000,
|
||||
message: 'Bad Request: No valid session ID provided'
|
||||
},
|
||||
id: null
|
||||
});
|
||||
return;
|
||||
}
|
||||
// Handle the request with existing transport - no need to reconnect
|
||||
// The existing transport is already connected to the server
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error handling MCP request:', error);
|
||||
if (!res.headersSent) {
|
||||
res.status(500).json({
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32603,
|
||||
message: 'Internal server error'
|
||||
},
|
||||
id: null
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
// Set up routes with conditional auth middleware
|
||||
if (useOAuth && authMiddleware) {
|
||||
app.post('/mcp', authMiddleware, mcpPostHandler);
|
||||
}
|
||||
else {
|
||||
app.post('/mcp', mcpPostHandler);
|
||||
}
|
||||
// Handle GET requests for SSE streams (using built-in support from StreamableHTTP)
|
||||
const mcpGetHandler = async (req, res) => {
|
||||
const sessionId = req.headers['mcp-session-id'];
|
||||
if (!sessionId || !transports[sessionId]) {
|
||||
res.status(400).send('Invalid or missing session ID');
|
||||
return;
|
||||
}
|
||||
if (useOAuth && req.auth) {
|
||||
console.log('Authenticated SSE connection from user:', req.auth);
|
||||
}
|
||||
// Check for Last-Event-ID header for resumability
|
||||
const lastEventId = req.headers['last-event-id'];
|
||||
if (lastEventId) {
|
||||
console.log(`Client reconnecting with Last-Event-ID: ${lastEventId}`);
|
||||
}
|
||||
else {
|
||||
console.log(`Establishing new SSE stream for session ${sessionId}`);
|
||||
}
|
||||
const transport = transports[sessionId];
|
||||
await transport.handleRequest(req, res);
|
||||
};
|
||||
// Set up GET route with conditional auth middleware
|
||||
if (useOAuth && authMiddleware) {
|
||||
app.get('/mcp', authMiddleware, mcpGetHandler);
|
||||
}
|
||||
else {
|
||||
app.get('/mcp', mcpGetHandler);
|
||||
}
|
||||
// Handle DELETE requests for session termination (according to MCP spec)
|
||||
const mcpDeleteHandler = async (req, res) => {
|
||||
const sessionId = req.headers['mcp-session-id'];
|
||||
if (!sessionId || !transports[sessionId]) {
|
||||
res.status(400).send('Invalid or missing session ID');
|
||||
return;
|
||||
}
|
||||
console.log(`Received session termination request for session ${sessionId}`);
|
||||
try {
|
||||
const transport = transports[sessionId];
|
||||
await transport.handleRequest(req, res);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error handling session termination:', error);
|
||||
if (!res.headersSent) {
|
||||
res.status(500).send('Error processing session termination');
|
||||
}
|
||||
}
|
||||
};
|
||||
// Set up DELETE route with conditional auth middleware
|
||||
if (useOAuth && authMiddleware) {
|
||||
app.delete('/mcp', authMiddleware, mcpDeleteHandler);
|
||||
}
|
||||
else {
|
||||
app.delete('/mcp', mcpDeleteHandler);
|
||||
}
|
||||
app.listen(MCP_PORT, error => {
|
||||
if (error) {
|
||||
console.error('Failed to start server:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(`MCP Streamable HTTP Server listening on port ${MCP_PORT}`);
|
||||
});
|
||||
// Handle server shutdown
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('Shutting down server...');
|
||||
// Close all active transports to properly clean up resources
|
||||
for (const sessionId in transports) {
|
||||
try {
|
||||
console.log(`Closing transport for session ${sessionId}`);
|
||||
await transports[sessionId].close();
|
||||
delete transports[sessionId];
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`Error closing transport for session ${sessionId}:`, error);
|
||||
}
|
||||
}
|
||||
console.log('Server shutdown complete');
|
||||
process.exit(0);
|
||||
});
|
||||
//# sourceMappingURL=simpleStreamableHttp.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleStreamableHttp.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleStreamableHttp.js.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
12
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleTaskInteractive.d.ts
generated
vendored
Normal file
12
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleTaskInteractive.d.ts
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Simple interactive task server demonstrating elicitation and sampling.
|
||||
*
|
||||
* This server demonstrates the task message queue pattern from the MCP Tasks spec:
|
||||
* - confirm_delete: Uses elicitation to ask the user for confirmation
|
||||
* - write_haiku: Uses sampling to request an LLM to generate content
|
||||
*
|
||||
* Both tools use the "call-now, fetch-later" pattern where the initial call
|
||||
* creates a task, and the result is fetched via tasks/result endpoint.
|
||||
*/
|
||||
export {};
|
||||
//# sourceMappingURL=simpleTaskInteractive.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleTaskInteractive.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleTaskInteractive.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"simpleTaskInteractive.d.ts","sourceRoot":"","sources":["../../../../src/examples/server/simpleTaskInteractive.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG"}
|
||||
598
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleTaskInteractive.js
generated
vendored
Normal file
598
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleTaskInteractive.js
generated
vendored
Normal file
@@ -0,0 +1,598 @@
|
||||
/**
|
||||
* Simple interactive task server demonstrating elicitation and sampling.
|
||||
*
|
||||
* This server demonstrates the task message queue pattern from the MCP Tasks spec:
|
||||
* - confirm_delete: Uses elicitation to ask the user for confirmation
|
||||
* - write_haiku: Uses sampling to request an LLM to generate content
|
||||
*
|
||||
* Both tools use the "call-now, fetch-later" pattern where the initial call
|
||||
* creates a task, and the result is fetched via tasks/result endpoint.
|
||||
*/
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { Server } from '../../server/index.js';
|
||||
import { createMcpExpressApp } from '../../server/express.js';
|
||||
import { StreamableHTTPServerTransport } from '../../server/streamableHttp.js';
|
||||
import { RELATED_TASK_META_KEY, ListToolsRequestSchema, CallToolRequestSchema, GetTaskRequestSchema, GetTaskPayloadRequestSchema } from '../../types.js';
|
||||
import { isTerminal } from '../../experimental/tasks/interfaces.js';
|
||||
import { InMemoryTaskStore } from '../../experimental/tasks/stores/in-memory.js';
|
||||
// ============================================================================
|
||||
// Resolver - Promise-like for passing results between async operations
|
||||
// ============================================================================
|
||||
class Resolver {
|
||||
constructor() {
|
||||
this._done = false;
|
||||
this._promise = new Promise((resolve, reject) => {
|
||||
this._resolve = resolve;
|
||||
this._reject = reject;
|
||||
});
|
||||
}
|
||||
setResult(value) {
|
||||
if (this._done)
|
||||
return;
|
||||
this._done = true;
|
||||
this._resolve(value);
|
||||
}
|
||||
setException(error) {
|
||||
if (this._done)
|
||||
return;
|
||||
this._done = true;
|
||||
this._reject(error);
|
||||
}
|
||||
wait() {
|
||||
return this._promise;
|
||||
}
|
||||
done() {
|
||||
return this._done;
|
||||
}
|
||||
}
|
||||
class TaskMessageQueueWithResolvers {
|
||||
constructor() {
|
||||
this.queues = new Map();
|
||||
this.waitResolvers = new Map();
|
||||
}
|
||||
getQueue(taskId) {
|
||||
let queue = this.queues.get(taskId);
|
||||
if (!queue) {
|
||||
queue = [];
|
||||
this.queues.set(taskId, queue);
|
||||
}
|
||||
return queue;
|
||||
}
|
||||
async enqueue(taskId, message, _sessionId, maxSize) {
|
||||
const queue = this.getQueue(taskId);
|
||||
if (maxSize !== undefined && queue.length >= maxSize) {
|
||||
throw new Error(`Task message queue overflow: queue size (${queue.length}) exceeds maximum (${maxSize})`);
|
||||
}
|
||||
queue.push(message);
|
||||
// Notify any waiters
|
||||
this.notifyWaiters(taskId);
|
||||
}
|
||||
async enqueueWithResolver(taskId, message, resolver, originalRequestId) {
|
||||
const queue = this.getQueue(taskId);
|
||||
const queuedMessage = {
|
||||
type: 'request',
|
||||
message,
|
||||
timestamp: Date.now(),
|
||||
resolver,
|
||||
originalRequestId
|
||||
};
|
||||
queue.push(queuedMessage);
|
||||
this.notifyWaiters(taskId);
|
||||
}
|
||||
async dequeue(taskId, _sessionId) {
|
||||
const queue = this.getQueue(taskId);
|
||||
return queue.shift();
|
||||
}
|
||||
async dequeueAll(taskId, _sessionId) {
|
||||
const queue = this.queues.get(taskId) ?? [];
|
||||
this.queues.delete(taskId);
|
||||
return queue;
|
||||
}
|
||||
async waitForMessage(taskId) {
|
||||
// Check if there are already messages
|
||||
const queue = this.getQueue(taskId);
|
||||
if (queue.length > 0)
|
||||
return;
|
||||
// Wait for a message to be added
|
||||
return new Promise(resolve => {
|
||||
let waiters = this.waitResolvers.get(taskId);
|
||||
if (!waiters) {
|
||||
waiters = [];
|
||||
this.waitResolvers.set(taskId, waiters);
|
||||
}
|
||||
waiters.push(resolve);
|
||||
});
|
||||
}
|
||||
notifyWaiters(taskId) {
|
||||
const waiters = this.waitResolvers.get(taskId);
|
||||
if (waiters) {
|
||||
this.waitResolvers.delete(taskId);
|
||||
for (const resolve of waiters) {
|
||||
resolve();
|
||||
}
|
||||
}
|
||||
}
|
||||
cleanup() {
|
||||
this.queues.clear();
|
||||
this.waitResolvers.clear();
|
||||
}
|
||||
}
|
||||
// ============================================================================
|
||||
// Extended task store with wait functionality
|
||||
// ============================================================================
|
||||
class TaskStoreWithNotifications extends InMemoryTaskStore {
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
this.updateResolvers = new Map();
|
||||
}
|
||||
async updateTaskStatus(taskId, status, statusMessage, sessionId) {
|
||||
await super.updateTaskStatus(taskId, status, statusMessage, sessionId);
|
||||
this.notifyUpdate(taskId);
|
||||
}
|
||||
async storeTaskResult(taskId, status, result, sessionId) {
|
||||
await super.storeTaskResult(taskId, status, result, sessionId);
|
||||
this.notifyUpdate(taskId);
|
||||
}
|
||||
async waitForUpdate(taskId) {
|
||||
return new Promise(resolve => {
|
||||
let waiters = this.updateResolvers.get(taskId);
|
||||
if (!waiters) {
|
||||
waiters = [];
|
||||
this.updateResolvers.set(taskId, waiters);
|
||||
}
|
||||
waiters.push(resolve);
|
||||
});
|
||||
}
|
||||
notifyUpdate(taskId) {
|
||||
const waiters = this.updateResolvers.get(taskId);
|
||||
if (waiters) {
|
||||
this.updateResolvers.delete(taskId);
|
||||
for (const resolve of waiters) {
|
||||
resolve();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// ============================================================================
|
||||
// Task Result Handler - delivers queued messages and routes responses
|
||||
// ============================================================================
|
||||
class TaskResultHandler {
|
||||
constructor(store, queue) {
|
||||
this.store = store;
|
||||
this.queue = queue;
|
||||
this.pendingRequests = new Map();
|
||||
}
|
||||
async handle(taskId, server, _sessionId) {
|
||||
while (true) {
|
||||
// Get fresh task state
|
||||
const task = await this.store.getTask(taskId);
|
||||
if (!task) {
|
||||
throw new Error(`Task not found: ${taskId}`);
|
||||
}
|
||||
// Dequeue and send all pending messages
|
||||
await this.deliverQueuedMessages(taskId, server, _sessionId);
|
||||
// If task is terminal, return result
|
||||
if (isTerminal(task.status)) {
|
||||
const result = await this.store.getTaskResult(taskId);
|
||||
// Add related-task metadata per spec
|
||||
return {
|
||||
...result,
|
||||
_meta: {
|
||||
...(result._meta || {}),
|
||||
[RELATED_TASK_META_KEY]: { taskId }
|
||||
}
|
||||
};
|
||||
}
|
||||
// Wait for task update or new message
|
||||
await this.waitForUpdate(taskId);
|
||||
}
|
||||
}
|
||||
async deliverQueuedMessages(taskId, server, _sessionId) {
|
||||
while (true) {
|
||||
const message = await this.queue.dequeue(taskId);
|
||||
if (!message)
|
||||
break;
|
||||
console.log(`[Server] Delivering queued ${message.type} message for task ${taskId}`);
|
||||
if (message.type === 'request') {
|
||||
const reqMessage = message;
|
||||
// Send the request via the server
|
||||
// Store the resolver so we can route the response back
|
||||
if (reqMessage.resolver && reqMessage.originalRequestId) {
|
||||
this.pendingRequests.set(reqMessage.originalRequestId, reqMessage.resolver);
|
||||
}
|
||||
// Send the message - for elicitation/sampling, we use the server's methods
|
||||
// But since we're in tasks/result context, we need to send via transport
|
||||
// This is simplified - in production you'd use proper message routing
|
||||
try {
|
||||
const request = reqMessage.message;
|
||||
let response;
|
||||
if (request.method === 'elicitation/create') {
|
||||
// Send elicitation request to client
|
||||
const params = request.params;
|
||||
response = await server.elicitInput(params);
|
||||
}
|
||||
else if (request.method === 'sampling/createMessage') {
|
||||
// Send sampling request to client
|
||||
const params = request.params;
|
||||
response = await server.createMessage(params);
|
||||
}
|
||||
else {
|
||||
throw new Error(`Unknown request method: ${request.method}`);
|
||||
}
|
||||
// Route response back to resolver
|
||||
if (reqMessage.resolver) {
|
||||
reqMessage.resolver.setResult(response);
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
if (reqMessage.resolver) {
|
||||
reqMessage.resolver.setException(error instanceof Error ? error : new Error(String(error)));
|
||||
}
|
||||
}
|
||||
}
|
||||
// For notifications, we'd send them too but this example focuses on requests
|
||||
}
|
||||
}
|
||||
async waitForUpdate(taskId) {
|
||||
// Race between store update and queue message
|
||||
await Promise.race([this.store.waitForUpdate(taskId), this.queue.waitForMessage(taskId)]);
|
||||
}
|
||||
routeResponse(requestId, response) {
|
||||
const resolver = this.pendingRequests.get(requestId);
|
||||
if (resolver && !resolver.done()) {
|
||||
this.pendingRequests.delete(requestId);
|
||||
resolver.setResult(response);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
routeError(requestId, error) {
|
||||
const resolver = this.pendingRequests.get(requestId);
|
||||
if (resolver && !resolver.done()) {
|
||||
this.pendingRequests.delete(requestId);
|
||||
resolver.setException(error);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// ============================================================================
|
||||
// Task Session - wraps server to enqueue requests during task execution
|
||||
// ============================================================================
|
||||
class TaskSession {
|
||||
constructor(server, taskId, store, queue) {
|
||||
this.server = server;
|
||||
this.taskId = taskId;
|
||||
this.store = store;
|
||||
this.queue = queue;
|
||||
this.requestCounter = 0;
|
||||
}
|
||||
nextRequestId() {
|
||||
return `task-${this.taskId}-${++this.requestCounter}`;
|
||||
}
|
||||
async elicit(message, requestedSchema) {
|
||||
// Update task status to input_required
|
||||
await this.store.updateTaskStatus(this.taskId, 'input_required');
|
||||
const requestId = this.nextRequestId();
|
||||
// Build the elicitation request with related-task metadata
|
||||
const params = {
|
||||
message,
|
||||
requestedSchema,
|
||||
mode: 'form',
|
||||
_meta: {
|
||||
[RELATED_TASK_META_KEY]: { taskId: this.taskId }
|
||||
}
|
||||
};
|
||||
const jsonrpcRequest = {
|
||||
jsonrpc: '2.0',
|
||||
id: requestId,
|
||||
method: 'elicitation/create',
|
||||
params
|
||||
};
|
||||
// Create resolver to wait for response
|
||||
const resolver = new Resolver();
|
||||
// Enqueue the request
|
||||
await this.queue.enqueueWithResolver(this.taskId, jsonrpcRequest, resolver, requestId);
|
||||
try {
|
||||
// Wait for response
|
||||
const response = await resolver.wait();
|
||||
// Update status back to working
|
||||
await this.store.updateTaskStatus(this.taskId, 'working');
|
||||
return response;
|
||||
}
|
||||
catch (error) {
|
||||
await this.store.updateTaskStatus(this.taskId, 'working');
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
async createMessage(messages, maxTokens) {
|
||||
// Update task status to input_required
|
||||
await this.store.updateTaskStatus(this.taskId, 'input_required');
|
||||
const requestId = this.nextRequestId();
|
||||
// Build the sampling request with related-task metadata
|
||||
const params = {
|
||||
messages,
|
||||
maxTokens,
|
||||
_meta: {
|
||||
[RELATED_TASK_META_KEY]: { taskId: this.taskId }
|
||||
}
|
||||
};
|
||||
const jsonrpcRequest = {
|
||||
jsonrpc: '2.0',
|
||||
id: requestId,
|
||||
method: 'sampling/createMessage',
|
||||
params
|
||||
};
|
||||
// Create resolver to wait for response
|
||||
const resolver = new Resolver();
|
||||
// Enqueue the request
|
||||
await this.queue.enqueueWithResolver(this.taskId, jsonrpcRequest, resolver, requestId);
|
||||
try {
|
||||
// Wait for response
|
||||
const response = await resolver.wait();
|
||||
// Update status back to working
|
||||
await this.store.updateTaskStatus(this.taskId, 'working');
|
||||
return response;
|
||||
}
|
||||
catch (error) {
|
||||
await this.store.updateTaskStatus(this.taskId, 'working');
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
// ============================================================================
|
||||
// Server Setup
|
||||
// ============================================================================
|
||||
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 8000;
|
||||
// Create shared stores
|
||||
const taskStore = new TaskStoreWithNotifications();
|
||||
const messageQueue = new TaskMessageQueueWithResolvers();
|
||||
const taskResultHandler = new TaskResultHandler(taskStore, messageQueue);
|
||||
// Track active task executions
|
||||
const activeTaskExecutions = new Map();
|
||||
// Create the server
|
||||
const createServer = () => {
|
||||
const server = new Server({ name: 'simple-task-interactive', version: '1.0.0' }, {
|
||||
capabilities: {
|
||||
tools: {},
|
||||
tasks: {
|
||||
requests: {
|
||||
tools: { call: {} }
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
// Register tools
|
||||
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
||||
return {
|
||||
tools: [
|
||||
{
|
||||
name: 'confirm_delete',
|
||||
description: 'Asks for confirmation before deleting (demonstrates elicitation)',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
filename: { type: 'string' }
|
||||
}
|
||||
},
|
||||
execution: { taskSupport: 'required' }
|
||||
},
|
||||
{
|
||||
name: 'write_haiku',
|
||||
description: 'Asks LLM to write a haiku (demonstrates sampling)',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
topic: { type: 'string' }
|
||||
}
|
||||
},
|
||||
execution: { taskSupport: 'required' }
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
// Handle tool calls
|
||||
server.setRequestHandler(CallToolRequestSchema, async (request, extra) => {
|
||||
const { name, arguments: args } = request.params;
|
||||
const taskParams = (request.params._meta?.task || request.params.task);
|
||||
// Validate task mode - these tools require tasks
|
||||
if (!taskParams) {
|
||||
throw new Error(`Tool ${name} requires task mode`);
|
||||
}
|
||||
// Create task
|
||||
const taskOptions = {
|
||||
ttl: taskParams.ttl,
|
||||
pollInterval: taskParams.pollInterval ?? 1000
|
||||
};
|
||||
const task = await taskStore.createTask(taskOptions, extra.requestId, request, extra.sessionId);
|
||||
console.log(`\n[Server] ${name} called, task created: ${task.taskId}`);
|
||||
// Start background task execution
|
||||
const taskExecution = (async () => {
|
||||
try {
|
||||
const taskSession = new TaskSession(server, task.taskId, taskStore, messageQueue);
|
||||
if (name === 'confirm_delete') {
|
||||
const filename = args?.filename ?? 'unknown.txt';
|
||||
console.log(`[Server] confirm_delete: asking about '${filename}'`);
|
||||
console.log('[Server] Sending elicitation request to client...');
|
||||
const result = await taskSession.elicit(`Are you sure you want to delete '${filename}'?`, {
|
||||
type: 'object',
|
||||
properties: {
|
||||
confirm: { type: 'boolean' }
|
||||
},
|
||||
required: ['confirm']
|
||||
});
|
||||
console.log(`[Server] Received elicitation response: action=${result.action}, content=${JSON.stringify(result.content)}`);
|
||||
let text;
|
||||
if (result.action === 'accept' && result.content) {
|
||||
const confirmed = result.content.confirm;
|
||||
text = confirmed ? `Deleted '${filename}'` : 'Deletion cancelled';
|
||||
}
|
||||
else {
|
||||
text = 'Deletion cancelled';
|
||||
}
|
||||
console.log(`[Server] Completing task with result: ${text}`);
|
||||
await taskStore.storeTaskResult(task.taskId, 'completed', {
|
||||
content: [{ type: 'text', text }]
|
||||
});
|
||||
}
|
||||
else if (name === 'write_haiku') {
|
||||
const topic = args?.topic ?? 'nature';
|
||||
console.log(`[Server] write_haiku: topic '${topic}'`);
|
||||
console.log('[Server] Sending sampling request to client...');
|
||||
const result = await taskSession.createMessage([
|
||||
{
|
||||
role: 'user',
|
||||
content: { type: 'text', text: `Write a haiku about ${topic}` }
|
||||
}
|
||||
], 50);
|
||||
let haiku = 'No response';
|
||||
if (result.content && 'text' in result.content) {
|
||||
haiku = result.content.text;
|
||||
}
|
||||
console.log(`[Server] Received sampling response: ${haiku.substring(0, 50)}...`);
|
||||
console.log('[Server] Completing task with haiku');
|
||||
await taskStore.storeTaskResult(task.taskId, 'completed', {
|
||||
content: [{ type: 'text', text: `Haiku:\n${haiku}` }]
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`[Server] Task ${task.taskId} failed:`, error);
|
||||
await taskStore.storeTaskResult(task.taskId, 'failed', {
|
||||
content: [{ type: 'text', text: `Error: ${error}` }],
|
||||
isError: true
|
||||
});
|
||||
}
|
||||
finally {
|
||||
activeTaskExecutions.delete(task.taskId);
|
||||
}
|
||||
})();
|
||||
activeTaskExecutions.set(task.taskId, {
|
||||
promise: taskExecution,
|
||||
server,
|
||||
sessionId: extra.sessionId ?? ''
|
||||
});
|
||||
return { task };
|
||||
});
|
||||
// Handle tasks/get
|
||||
server.setRequestHandler(GetTaskRequestSchema, async (request) => {
|
||||
const { taskId } = request.params;
|
||||
const task = await taskStore.getTask(taskId);
|
||||
if (!task) {
|
||||
throw new Error(`Task ${taskId} not found`);
|
||||
}
|
||||
return task;
|
||||
});
|
||||
// Handle tasks/result
|
||||
server.setRequestHandler(GetTaskPayloadRequestSchema, async (request, extra) => {
|
||||
const { taskId } = request.params;
|
||||
console.log(`[Server] tasks/result called for task ${taskId}`);
|
||||
return taskResultHandler.handle(taskId, server, extra.sessionId ?? '');
|
||||
});
|
||||
return server;
|
||||
};
|
||||
// ============================================================================
|
||||
// Express App Setup
|
||||
// ============================================================================
|
||||
const app = createMcpExpressApp();
|
||||
// Map to store transports by session ID
|
||||
const transports = {};
|
||||
// Helper to check if request is initialize
|
||||
const isInitializeRequest = (body) => {
|
||||
return typeof body === 'object' && body !== null && 'method' in body && body.method === 'initialize';
|
||||
};
|
||||
// MCP POST endpoint
|
||||
app.post('/mcp', async (req, res) => {
|
||||
const sessionId = req.headers['mcp-session-id'];
|
||||
try {
|
||||
let transport;
|
||||
if (sessionId && transports[sessionId]) {
|
||||
transport = transports[sessionId];
|
||||
}
|
||||
else if (!sessionId && isInitializeRequest(req.body)) {
|
||||
transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: () => randomUUID(),
|
||||
onsessioninitialized: sid => {
|
||||
console.log(`Session initialized: ${sid}`);
|
||||
transports[sid] = transport;
|
||||
}
|
||||
});
|
||||
transport.onclose = () => {
|
||||
const sid = transport.sessionId;
|
||||
if (sid && transports[sid]) {
|
||||
console.log(`Transport closed for session ${sid}`);
|
||||
delete transports[sid];
|
||||
}
|
||||
};
|
||||
const server = createServer();
|
||||
await server.connect(transport);
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
res.status(400).json({
|
||||
jsonrpc: '2.0',
|
||||
error: { code: -32000, message: 'Bad Request: No valid session ID' },
|
||||
id: null
|
||||
});
|
||||
return;
|
||||
}
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error handling MCP request:', error);
|
||||
if (!res.headersSent) {
|
||||
res.status(500).json({
|
||||
jsonrpc: '2.0',
|
||||
error: { code: -32603, message: 'Internal server error' },
|
||||
id: null
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
// Handle GET requests for SSE streams
|
||||
app.get('/mcp', async (req, res) => {
|
||||
const sessionId = req.headers['mcp-session-id'];
|
||||
if (!sessionId || !transports[sessionId]) {
|
||||
res.status(400).send('Invalid or missing session ID');
|
||||
return;
|
||||
}
|
||||
const transport = transports[sessionId];
|
||||
await transport.handleRequest(req, res);
|
||||
});
|
||||
// Handle DELETE requests for session termination
|
||||
app.delete('/mcp', async (req, res) => {
|
||||
const sessionId = req.headers['mcp-session-id'];
|
||||
if (!sessionId || !transports[sessionId]) {
|
||||
res.status(400).send('Invalid or missing session ID');
|
||||
return;
|
||||
}
|
||||
console.log(`Session termination request: ${sessionId}`);
|
||||
const transport = transports[sessionId];
|
||||
await transport.handleRequest(req, res);
|
||||
});
|
||||
// Start server
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Starting server on http://localhost:${PORT}/mcp`);
|
||||
console.log('\nAvailable tools:');
|
||||
console.log(' - confirm_delete: Demonstrates elicitation (asks user y/n)');
|
||||
console.log(' - write_haiku: Demonstrates sampling (requests LLM completion)');
|
||||
});
|
||||
// Handle shutdown
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('\nShutting down server...');
|
||||
for (const sessionId of Object.keys(transports)) {
|
||||
try {
|
||||
await transports[sessionId].close();
|
||||
delete transports[sessionId];
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`Error closing session ${sessionId}:`, error);
|
||||
}
|
||||
}
|
||||
taskStore.cleanup();
|
||||
messageQueue.cleanup();
|
||||
console.log('Server shutdown complete');
|
||||
process.exit(0);
|
||||
});
|
||||
//# sourceMappingURL=simpleTaskInteractive.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleTaskInteractive.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/simpleTaskInteractive.js.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/sseAndStreamableHttpCompatibleServer.d.ts
generated
vendored
Normal file
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/sseAndStreamableHttpCompatibleServer.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=sseAndStreamableHttpCompatibleServer.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/sseAndStreamableHttpCompatibleServer.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/sseAndStreamableHttpCompatibleServer.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"sseAndStreamableHttpCompatibleServer.d.ts","sourceRoot":"","sources":["../../../../src/examples/server/sseAndStreamableHttpCompatibleServer.ts"],"names":[],"mappings":""}
|
||||
231
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/sseAndStreamableHttpCompatibleServer.js
generated
vendored
Normal file
231
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/sseAndStreamableHttpCompatibleServer.js
generated
vendored
Normal file
@@ -0,0 +1,231 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { McpServer } from '../../server/mcp.js';
|
||||
import { StreamableHTTPServerTransport } from '../../server/streamableHttp.js';
|
||||
import { SSEServerTransport } from '../../server/sse.js';
|
||||
import * as z from 'zod/v4';
|
||||
import { isInitializeRequest } from '../../types.js';
|
||||
import { InMemoryEventStore } from '../shared/inMemoryEventStore.js';
|
||||
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-11-25)
|
||||
*
|
||||
* It maintains a single MCP server instance but exposes two transport options:
|
||||
* - /mcp: The new Streamable HTTP endpoint (supports GET/POST/DELETE)
|
||||
* - /sse: The deprecated SSE endpoint for older clients (GET to establish stream)
|
||||
* - /messages: The deprecated POST endpoint for older clients (POST to send messages)
|
||||
*/
|
||||
const getServer = () => {
|
||||
const server = new McpServer({
|
||||
name: 'backwards-compatible-server',
|
||||
version: '1.0.0'
|
||||
}, { capabilities: { logging: {} } });
|
||||
// Register a simple tool that sends notifications over time
|
||||
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 server.sendLoggingMessage({
|
||||
level: 'info',
|
||||
data: `Periodic notification #${counter} at ${new Date().toISOString()}`
|
||||
}, extra.sessionId);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error sending notification:', error);
|
||||
}
|
||||
// Wait for the specified interval
|
||||
await sleep(interval);
|
||||
}
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Started sending periodic notifications every ${interval}ms`
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
return server;
|
||||
};
|
||||
// Create Express application
|
||||
const app = createMcpExpressApp();
|
||||
// Store transports by session ID
|
||||
const transports = {};
|
||||
//=============================================================================
|
||||
// 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) => {
|
||||
console.log(`Received ${req.method} request to /mcp`);
|
||||
try {
|
||||
// Check for existing session ID
|
||||
const sessionId = req.headers['mcp-session-id'];
|
||||
let transport;
|
||||
if (sessionId && transports[sessionId]) {
|
||||
// Check if the transport is of the correct type
|
||||
const existingTransport = transports[sessionId];
|
||||
if (existingTransport instanceof StreamableHTTPServerTransport) {
|
||||
// Reuse existing transport
|
||||
transport = existingTransport;
|
||||
}
|
||||
else {
|
||||
// Transport exists but is not a StreamableHTTPServerTransport (could be SSEServerTransport)
|
||||
res.status(400).json({
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32000,
|
||||
message: 'Bad Request: Session exists but uses a different transport protocol'
|
||||
},
|
||||
id: null
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (!sessionId && req.method === 'POST' && isInitializeRequest(req.body)) {
|
||||
const eventStore = new InMemoryEventStore();
|
||||
transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: () => randomUUID(),
|
||||
eventStore, // Enable resumability
|
||||
onsessioninitialized: sessionId => {
|
||||
// Store the transport by session ID when session is initialized
|
||||
console.log(`StreamableHTTP session initialized with ID: ${sessionId}`);
|
||||
transports[sessionId] = transport;
|
||||
}
|
||||
});
|
||||
// Set up onclose handler to clean up transport when closed
|
||||
transport.onclose = () => {
|
||||
const sid = transport.sessionId;
|
||||
if (sid && transports[sid]) {
|
||||
console.log(`Transport closed for session ${sid}, removing from transports map`);
|
||||
delete transports[sid];
|
||||
}
|
||||
};
|
||||
// Connect the transport to the MCP server
|
||||
const server = getServer();
|
||||
await server.connect(transport);
|
||||
}
|
||||
else {
|
||||
// Invalid request - no session ID or not initialization request
|
||||
res.status(400).json({
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32000,
|
||||
message: 'Bad Request: No valid session ID provided'
|
||||
},
|
||||
id: null
|
||||
});
|
||||
return;
|
||||
}
|
||||
// Handle the request with the transport
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error handling MCP request:', error);
|
||||
if (!res.headersSent) {
|
||||
res.status(500).json({
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32603,
|
||||
message: 'Internal server error'
|
||||
},
|
||||
id: null
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
//=============================================================================
|
||||
// DEPRECATED HTTP+SSE TRANSPORT (PROTOCOL VERSION 2024-11-05)
|
||||
//=============================================================================
|
||||
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', () => {
|
||||
delete transports[transport.sessionId];
|
||||
});
|
||||
const server = getServer();
|
||||
await server.connect(transport);
|
||||
});
|
||||
app.post('/messages', async (req, res) => {
|
||||
const sessionId = req.query.sessionId;
|
||||
let transport;
|
||||
const existingTransport = transports[sessionId];
|
||||
if (existingTransport instanceof SSEServerTransport) {
|
||||
// Reuse existing transport
|
||||
transport = existingTransport;
|
||||
}
|
||||
else {
|
||||
// Transport exists but is not a SSEServerTransport (could be StreamableHTTPServerTransport)
|
||||
res.status(400).json({
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32000,
|
||||
message: 'Bad Request: Session exists but uses a different transport protocol'
|
||||
},
|
||||
id: null
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (transport) {
|
||||
await transport.handlePostMessage(req, res, req.body);
|
||||
}
|
||||
else {
|
||||
res.status(400).send('No transport found for sessionId');
|
||||
}
|
||||
});
|
||||
// Start the server
|
||||
const PORT = 3000;
|
||||
app.listen(PORT, error => {
|
||||
if (error) {
|
||||
console.error('Failed to start server:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(`Backwards compatible MCP server listening on port ${PORT}`);
|
||||
console.log(`
|
||||
==============================================
|
||||
SUPPORTED TRANSPORT OPTIONS:
|
||||
|
||||
1. Streamable Http(Protocol version: 2025-11-25)
|
||||
Endpoint: /mcp
|
||||
Methods: GET, POST, DELETE
|
||||
Usage:
|
||||
- Initialize with POST to /mcp
|
||||
- Establish SSE stream with GET to /mcp
|
||||
- Send requests with POST to /mcp
|
||||
- Terminate session with DELETE to /mcp
|
||||
|
||||
2. Http + SSE (Protocol version: 2024-11-05)
|
||||
Endpoints: /sse (GET) and /messages (POST)
|
||||
Usage:
|
||||
- Establish SSE stream with GET to /sse
|
||||
- Send requests with POST to /messages?sessionId=<id>
|
||||
==============================================
|
||||
`);
|
||||
});
|
||||
// Handle server shutdown
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('Shutting down server...');
|
||||
// Close all active transports to properly clean up resources
|
||||
for (const sessionId in transports) {
|
||||
try {
|
||||
console.log(`Closing transport for session ${sessionId}`);
|
||||
await transports[sessionId].close();
|
||||
delete transports[sessionId];
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`Error closing transport for session ${sessionId}:`, error);
|
||||
}
|
||||
}
|
||||
console.log('Server shutdown complete');
|
||||
process.exit(0);
|
||||
});
|
||||
//# sourceMappingURL=sseAndStreamableHttpCompatibleServer.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/sseAndStreamableHttpCompatibleServer.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/sseAndStreamableHttpCompatibleServer.js.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/ssePollingExample.d.ts
generated
vendored
Normal file
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/ssePollingExample.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=ssePollingExample.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/ssePollingExample.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/ssePollingExample.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"ssePollingExample.d.ts","sourceRoot":"","sources":["../../../../src/examples/server/ssePollingExample.ts"],"names":[],"mappings":""}
|
||||
102
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/ssePollingExample.js
generated
vendored
Normal file
102
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/ssePollingExample.js
generated
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { McpServer } from '../../server/mcp.js';
|
||||
import { createMcpExpressApp } from '../../server/express.js';
|
||||
import { StreamableHTTPServerTransport } from '../../server/streamableHttp.js';
|
||||
import { InMemoryEventStore } from '../shared/inMemoryEventStore.js';
|
||||
import cors from 'cors';
|
||||
// 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: 'sse-polling-example',
|
||||
version: '1.0.0'
|
||||
}, {
|
||||
capabilities: { logging: {} }
|
||||
});
|
||||
// Register a long-running tool that demonstrates server-initiated disconnect
|
||||
server.tool('long-task', 'A long-running task that sends progress updates. Server will disconnect mid-task to demonstrate polling.', {}, async (_args, extra) => {
|
||||
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
||||
console.log(`[${extra.sessionId}] Starting long-task...`);
|
||||
// Send first progress notification
|
||||
await server.sendLoggingMessage({
|
||||
level: 'info',
|
||||
data: 'Progress: 25% - Starting work...'
|
||||
}, extra.sessionId);
|
||||
await sleep(1000);
|
||||
// Send second progress notification
|
||||
await server.sendLoggingMessage({
|
||||
level: 'info',
|
||||
data: 'Progress: 50% - Halfway there...'
|
||||
}, extra.sessionId);
|
||||
await sleep(1000);
|
||||
// Server decides to disconnect the client to free resources
|
||||
// Client will reconnect via GET with Last-Event-ID after the transport's retryInterval
|
||||
// Use extra.closeSSEStream callback - available when eventStore is configured
|
||||
if (extra.closeSSEStream) {
|
||||
console.log(`[${extra.sessionId}] Closing SSE stream to trigger client polling...`);
|
||||
extra.closeSSEStream();
|
||||
}
|
||||
// Continue processing while client is disconnected
|
||||
// Events are stored in eventStore and will be replayed on reconnect
|
||||
await sleep(500);
|
||||
await server.sendLoggingMessage({
|
||||
level: 'info',
|
||||
data: 'Progress: 75% - Almost done (sent while client disconnected)...'
|
||||
}, extra.sessionId);
|
||||
await sleep(500);
|
||||
await server.sendLoggingMessage({
|
||||
level: 'info',
|
||||
data: 'Progress: 100% - Complete!'
|
||||
}, extra.sessionId);
|
||||
console.log(`[${extra.sessionId}] Task complete`);
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: 'Long task completed successfully!'
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
return server;
|
||||
};
|
||||
// Set up Express app
|
||||
const app = createMcpExpressApp();
|
||||
app.use(cors());
|
||||
// Create event store for resumability
|
||||
const eventStore = new InMemoryEventStore();
|
||||
// Track transports by session ID for session reuse
|
||||
const transports = new Map();
|
||||
// Handle all MCP requests
|
||||
app.all('/mcp', async (req, res) => {
|
||||
const sessionId = req.headers['mcp-session-id'];
|
||||
// Reuse existing transport or create new one
|
||||
let transport = sessionId ? transports.get(sessionId) : undefined;
|
||||
if (!transport) {
|
||||
transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: () => randomUUID(),
|
||||
eventStore,
|
||||
retryInterval: 2000, // Default retry interval for priming events
|
||||
onsessioninitialized: id => {
|
||||
console.log(`[${id}] Session initialized`);
|
||||
transports.set(id, transport);
|
||||
}
|
||||
});
|
||||
// Create a new server per session and connect it to the transport
|
||||
const server = getServer();
|
||||
await server.connect(transport);
|
||||
}
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
});
|
||||
// Start the server
|
||||
const PORT = 3001;
|
||||
app.listen(PORT, () => {
|
||||
console.log(`SSE Polling Example Server running on http://localhost:${PORT}/mcp`);
|
||||
console.log('');
|
||||
console.log('This server demonstrates SEP-1699 SSE polling:');
|
||||
console.log('- retryInterval: 2000ms (client waits 2s before reconnecting)');
|
||||
console.log('- eventStore: InMemoryEventStore (events are persisted for replay)');
|
||||
console.log('');
|
||||
console.log('Try calling the "long-task" tool to see server-initiated disconnect in action.');
|
||||
});
|
||||
//# sourceMappingURL=ssePollingExample.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/ssePollingExample.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/ssePollingExample.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"ssePollingExample.js","sourceRoot":"","sources":["../../../../src/examples/server/ssePollingExample.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAC9D,OAAO,EAAE,6BAA6B,EAAE,MAAM,gCAAgC,CAAC;AAE/E,OAAO,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AACrE,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,kDAAkD;AAClD,yFAAyF;A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AAE,4CAA4C;YACjE,oBAAoB,EAAE,EAAE,CAAC,EAAE;gBACvB,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,uBAAuB,CAAC,CAAC;gBAC3C,UAAU,CAAC,GAAG,CAAC,EAAE,EAAE,SAAU,CAAC,CAAC;YACnC,CAAC;SACJ,CAAC,CAAC;QAEH,kEAAkE;QAClE,MAAM,MAAM,GAAG,SAAS,EAAE,CAAC;QAC3B,MAAM,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACpC,CAAC;IAED,MAAM,SAAS,CAAC,aAAa,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC;AACtD,CAAC,CAAC,CAAC;AAEH,mBAAmB;AACnB,MAAM,IAAI,GAAG,IAAI,CAAC;AAClB,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE;IAClB,OAAO,CAAC,GAAG,CAAC,0DAA0D,IAAI,MAAM,CAAC,CAAC;IAClF,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAChB,OAAO,CAAC,GAAG,CAAC,gDAAgD,CAAC,CAAC;IAC9D,OAAO,CAAC,GAAG,CAAC,+DAA+D,CAAC,CAAC;IAC7E,OAAO,CAAC,GAAG,CAAC,oEAAoE,CAAC,CAAC;IAClF,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAChB,OAAO,CAAC,GAAG,CAAC,gFAAgF,CAAC,CAAC;AAClG,CAAC,CAAC,CAAC"}
|
||||
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/standaloneSseWithGetStreamableHttp.d.ts
generated
vendored
Normal file
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/standaloneSseWithGetStreamableHttp.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=standaloneSseWithGetStreamableHttp.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/standaloneSseWithGetStreamableHttp.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/standaloneSseWithGetStreamableHttp.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"standaloneSseWithGetStreamableHttp.d.ts","sourceRoot":"","sources":["../../../../src/examples/server/standaloneSseWithGetStreamableHttp.ts"],"names":[],"mappings":""}
|
||||
122
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/standaloneSseWithGetStreamableHttp.js
generated
vendored
Normal file
122
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/standaloneSseWithGetStreamableHttp.js
generated
vendored
Normal file
@@ -0,0 +1,122 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { McpServer } from '../../server/mcp.js';
|
||||
import { StreamableHTTPServerTransport } from '../../server/streamableHttp.js';
|
||||
import { isInitializeRequest } from '../../types.js';
|
||||
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 app = createMcpExpressApp();
|
||||
app.post('/mcp', async (req, res) => {
|
||||
console.log('Received MCP request:', req.body);
|
||||
try {
|
||||
// Check for existing session ID
|
||||
const sessionId = req.headers['mcp-session-id'];
|
||||
let transport;
|
||||
if (sessionId && transports[sessionId]) {
|
||||
// Reuse existing transport
|
||||
transport = transports[sessionId];
|
||||
}
|
||||
else if (!sessionId && isInitializeRequest(req.body)) {
|
||||
// New initialization request
|
||||
transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: () => randomUUID(),
|
||||
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;
|
||||
}
|
||||
});
|
||||
// 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);
|
||||
return; // Already handled
|
||||
}
|
||||
else {
|
||||
// Invalid request - no session ID or not initialization request
|
||||
res.status(400).json({
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32000,
|
||||
message: 'Bad Request: No valid session ID provided'
|
||||
},
|
||||
id: null
|
||||
});
|
||||
return;
|
||||
}
|
||||
// Handle the request with existing transport
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error handling MCP request:', error);
|
||||
if (!res.headersSent) {
|
||||
res.status(500).json({
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32603,
|
||||
message: 'Internal server error'
|
||||
},
|
||||
id: null
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
// Handle GET requests for SSE streams (now using built-in support from StreamableHTTP)
|
||||
app.get('/mcp', async (req, res) => {
|
||||
const sessionId = req.headers['mcp-session-id'];
|
||||
if (!sessionId || !transports[sessionId]) {
|
||||
res.status(400).send('Invalid or missing session ID');
|
||||
return;
|
||||
}
|
||||
console.log(`Establishing SSE stream for session ${sessionId}`);
|
||||
const transport = transports[sessionId];
|
||||
await transport.handleRequest(req, res);
|
||||
});
|
||||
// Start the server
|
||||
const PORT = 3000;
|
||||
app.listen(PORT, error => {
|
||||
if (error) {
|
||||
console.error('Failed to start server:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(`Server listening on port ${PORT}`);
|
||||
});
|
||||
// Handle server shutdown
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('Shutting down server...');
|
||||
for (const sessionId in transports) {
|
||||
await transports[sessionId].close();
|
||||
delete transports[sessionId];
|
||||
}
|
||||
process.exit(0);
|
||||
});
|
||||
//# sourceMappingURL=standaloneSseWithGetStreamableHttp.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/standaloneSseWithGetStreamableHttp.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/standaloneSseWithGetStreamableHttp.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"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,GAA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,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,KAAK,IAAI,EAAE;IAC5B,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAC;IACvC,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;QACjC,MAAM,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"}
|
||||
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/toolWithSampleServer.d.ts
generated
vendored
Normal file
2
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/toolWithSampleServer.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=toolWithSampleServer.d.ts.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/toolWithSampleServer.d.ts.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/toolWithSampleServer.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"toolWithSampleServer.d.ts","sourceRoot":"","sources":["../../../../src/examples/server/toolWithSampleServer.ts"],"names":[],"mappings":""}
|
||||
48
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/toolWithSampleServer.js
generated
vendored
Normal file
48
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/toolWithSampleServer.js
generated
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
// Run with: npx tsx src/examples/server/toolWithSampleServer.ts
|
||||
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'
|
||||
});
|
||||
// Tool that uses LLM sampling to summarize any text
|
||||
mcpServer.registerTool('summarize', {
|
||||
description: 'Summarize any text using an LLM',
|
||||
inputSchema: {
|
||||
text: z.string().describe('Text to summarize')
|
||||
}
|
||||
}, async ({ text }) => {
|
||||
// Call the LLM through MCP sampling
|
||||
const response = await mcpServer.server.createMessage({
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: {
|
||||
type: 'text',
|
||||
text: `Please summarize the following text concisely:\n\n${text}`
|
||||
}
|
||||
}
|
||||
],
|
||||
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'
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
async function main() {
|
||||
const transport = new StdioServerTransport();
|
||||
await mcpServer.connect(transport);
|
||||
console.log('MCP server is running...');
|
||||
}
|
||||
main().catch(error => {
|
||||
console.error('Server error:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
//# sourceMappingURL=toolWithSampleServer.js.map
|
||||
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/toolWithSampleServer.js.map
generated
vendored
Normal file
1
node_modules/@modelcontextprotocol/sdk/dist/esm/examples/server/toolWithSampleServer.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"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,EAAE;IACf,oCAAoC;IACpC,MAAM,QAAQ,GAAG,MAAM,SAAS,CAAC,MAAM,CAAC,aAAa,CAAC;QAClD,QAAQ,EAAE;YACN;gBACI,IAAI,EAAE,MAAM;gBACZ,OAAO,EAAE;oBACL,IAAI,EAAE,MAAM;oBACZ,IAAI,EAAE,qDAAqD,IAAI,EAAE;iBACpE;aACJ;SACJ;QACD,SAAS,EAAE,GAAG;KACjB,CAAC,CAAC;IAEH,2FAA2F;IAC3F,OAAO;QACH,OAAO,EAAE;YACL;gBACI,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;aAChG;SACJ;KACJ,CAAC;AACN,CAAC,CACJ,CAAC;AAEF,KAAK,UAAU,IAAI;IACf,MAAM,SAAS,GAAG,IAAI,oBAAoB,EAAE,CAAC;IAC7C,MAAM,SAAS,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACnC,OAAO,CAAC,GAAG,CAAC,0BAA0B,CAAC,CAAC;AAC5C,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"}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user