Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions examples/client/src/elicitationUrlExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import type {
import {
CallToolResultSchema,
Client,
ElicitationCompleteNotificationSchema,
ElicitRequestSchema,
ErrorCode,
getDisplayName,
ListToolsResultSchema,
Expand Down Expand Up @@ -560,10 +558,10 @@ async function connect(url?: string): Promise<void> {
console.log('👤 Client created');

// Set up elicitation request handler with proper validation
client.setRequestHandler(ElicitRequestSchema, elicitationRequestHandler);
client.setRequestHandler('elicitation/create', elicitationRequestHandler);

// Set up notification handler for elicitation completion
client.setNotificationHandler(ElicitationCompleteNotificationSchema, notification => {
client.setNotificationHandler('notifications/elicitation/complete', notification => {
const { elicitationId } = notification.params;
const pending = pendingURLElicitations.get(elicitationId);
if (pending) {
Expand Down
9 changes: 2 additions & 7 deletions examples/client/src/multipleClientsParallel.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import type { CallToolRequest, CallToolResult } from '@modelcontextprotocol/client';
import {
CallToolResultSchema,
Client,
LoggingMessageNotificationSchema,
StreamableHTTPClientTransport
} from '@modelcontextprotocol/client';
import { CallToolResultSchema, Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client';

/**
* Multiple Clients MCP Example
Expand Down Expand Up @@ -42,7 +37,7 @@ async function createAndRunClient(config: ClientConfig): Promise<{ id: string; r
};

// Set up client-specific notification handler
client.setNotificationHandler(LoggingMessageNotificationSchema, notification => {
client.setNotificationHandler('notifications/message', notification => {
console.log(`[${config.id}] Notification: ${notification.params.data}`);
});

Expand Down
10 changes: 2 additions & 8 deletions examples/client/src/parallelToolCallsClient.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import type { CallToolResult, ListToolsRequest } from '@modelcontextprotocol/client';
import {
CallToolResultSchema,
Client,
ListToolsResultSchema,
LoggingMessageNotificationSchema,
StreamableHTTPClientTransport
} from '@modelcontextprotocol/client';
import { CallToolResultSchema, Client, ListToolsResultSchema, StreamableHTTPClientTransport } from '@modelcontextprotocol/client';

/**
* Parallel Tool Calls MCP Client
Expand Down Expand Up @@ -44,7 +38,7 @@ async function main(): Promise<void> {
console.log('Successfully connected to MCP server');

// Set up notification handler with caller identification
client.setNotificationHandler(LoggingMessageNotificationSchema, notification => {
client.setNotificationHandler('notifications/message', notification => {
console.log(`Notification: ${notification.params.data}`);
});

Expand Down
21 changes: 9 additions & 12 deletions examples/client/src/simpleStreamableHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@ import type {
import {
CallToolResultSchema,
Client,
ElicitRequestSchema,
ErrorCode,
getDisplayName,
GetPromptResultSchema,
ListPromptsResultSchema,
ListResourcesResultSchema,
ListToolsResultSchema,
LoggingMessageNotificationSchema,
McpError,
ReadResourceResultSchema,
RELATED_TASK_META_KEY,
ResourceListChangedNotificationSchema,
StreamableHTTPClientTransport
} from '@modelcontextprotocol/client';
import { Ajv } from 'ajv';
Expand Down Expand Up @@ -271,7 +268,7 @@ async function connect(url?: string): Promise<void> {
};

// Set up elicitation request handler with proper validation
client.setRequestHandler(ElicitRequestSchema, async request => {
client.setRequestHandler('elicitation/create', async request => {
if (request.params.mode !== 'form') {
throw new McpError(ErrorCode.InvalidParams, `Unsupported elicitation mode: ${request.params.mode}`);
}
Expand All @@ -296,7 +293,7 @@ async function connect(url?: string): Promise<void> {
attempts++;
console.log(`\nPlease provide the following information (attempt ${attempts}/${maxAttempts}):`);

const content: Record<string, unknown> = {};
const content: Record<string, string | number | boolean | string[]> = {};
let inputCancelled = false;

// Collect input for each field
Expand Down Expand Up @@ -360,15 +357,15 @@ async function connect(url?: string): Promise<void> {
// Parse and validate the input
try {
if (answer === '' && field.default !== undefined) {
content[fieldName] = field.default;
content[fieldName] = field.default as string | number | boolean | string[];
} 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: unknown;
let parsedValue: string | number | boolean | string[];

switch (field.type) {
case 'boolean': {
Expand Down Expand Up @@ -414,7 +411,7 @@ async function connect(url?: string): Promise<void> {
}

if (inputCancelled) {
return { action: 'cancel' };
return { action: 'cancel' as const };
}

// If we didn't complete all fields due to an error, try again
Expand All @@ -427,7 +424,7 @@ async function connect(url?: string): Promise<void> {
continue;
} else {
console.log('Maximum attempts reached. Declining request.');
return { action: 'decline' };
return { action: 'decline' as const };
}
}

Expand All @@ -446,7 +443,7 @@ async function connect(url?: string): Promise<void> {
continue;
} else {
console.log('Maximum attempts reached. Declining request.');
return { action: 'decline' };
return { action: 'decline' as const };
}
}

Expand Down Expand Up @@ -496,14 +493,14 @@ async function connect(url?: string): Promise<void> {
});

// Set up notification handlers
client.setNotificationHandler(LoggingMessageNotificationSchema, notification => {
client.setNotificationHandler('notifications/message', notification => {
notificationCount++;
console.log(`\nNotification #${notificationCount}: ${notification.params.level} - ${notification.params.data}`);
// Re-display the prompt
process.stdout.write('> ');
});

client.setNotificationHandler(ResourceListChangedNotificationSchema, async _ => {
client.setNotificationHandler('notifications/resources/list_changed', async _ => {
console.log(`\nResource list changed notification received!`);
try {
if (!client) {
Expand Down
16 changes: 4 additions & 12 deletions examples/client/src/simpleTaskInteractiveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,7 @@
import { createInterface } from 'node:readline';

import type { CreateMessageRequest, CreateMessageResult, TextContent } from '@modelcontextprotocol/client';
import {
CallToolResultSchema,
Client,
CreateMessageRequestSchema,
ElicitRequestSchema,
ErrorCode,
McpError,
StreamableHTTPClientTransport
} from '@modelcontextprotocol/client';
import { CallToolResultSchema, Client, ErrorCode, McpError, StreamableHTTPClientTransport } from '@modelcontextprotocol/client';

// Create readline interface for user input
const readline = createInterface({
Expand All @@ -43,7 +35,7 @@ async function elicitationCallback(params: {
mode?: string;
message: string;
requestedSchema?: object;
}): Promise<{ action: string; content?: Record<string, unknown> }> {
}): Promise<{ action: 'accept' | 'cancel' | 'decline'; content?: Record<string, string | number | boolean | string[]> }> {
console.log(`\n[Elicitation] Server asks: ${params.message}`);

// Simple terminal prompt for y/n
Expand Down Expand Up @@ -102,15 +94,15 @@ async function run(url: string): Promise<void> {
);

// Set up elicitation request handler
client.setRequestHandler(ElicitRequestSchema, async request => {
client.setRequestHandler('elicitation/create', 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 => {
client.setRequestHandler('sampling/createMessage', async request => {
return samplingCallback(request.params) as unknown as ReturnType<typeof samplingCallback>;
});

Expand Down
9 changes: 2 additions & 7 deletions examples/client/src/ssePollingClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@
* Run with: pnpm tsx src/ssePollingClient.ts
* Requires: ssePollingExample.ts server running on port 3001
*/
import {
CallToolResultSchema,
Client,
LoggingMessageNotificationSchema,
StreamableHTTPClientTransport
} from '@modelcontextprotocol/client';
import { CallToolResultSchema, Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client';

const SERVER_URL = 'http://localhost:3001/mcp';

Expand Down Expand Up @@ -60,7 +55,7 @@ async function main(): Promise<void> {
});

// Set up notification handler to receive progress updates
client.setNotificationHandler(LoggingMessageNotificationSchema, notification => {
client.setNotificationHandler('notifications/message', notification => {
const data = notification.params.data;
console.log(`[Notification] ${data}`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
CallToolResultSchema,
Client,
ListToolsResultSchema,
LoggingMessageNotificationSchema,
SSEClientTransport,
StreamableHTTPClientTransport
} from '@modelcontextprotocol/client';
Expand Down Expand Up @@ -39,7 +38,7 @@ async function main(): Promise<void> {
transport = connection.transport;

// Set up notification handler
client.setNotificationHandler(LoggingMessageNotificationSchema, notification => {
client.setNotificationHandler('notifications/message', notification => {
console.log(`Notification: ${notification.params.level} - ${notification.params.data}`);
});

Expand Down
19 changes: 5 additions & 14 deletions examples/server/src/simpleTaskInteractive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,7 @@ import type {
TextContent,
Tool
} from '@modelcontextprotocol/server';
import {
CallToolRequestSchema,
GetTaskPayloadRequestSchema,
GetTaskRequestSchema,
InMemoryTaskStore,
isTerminal,
ListToolsRequestSchema,
RELATED_TASK_META_KEY,
Server
} from '@modelcontextprotocol/server';
import { InMemoryTaskStore, isTerminal, RELATED_TASK_META_KEY, Server } from '@modelcontextprotocol/server';
import type { Request, Response } from 'express';

// ============================================================================
Expand Down Expand Up @@ -486,7 +477,7 @@ const createServer = (): Server => {
);

// Register tools
server.setRequestHandler(ListToolsRequestSchema, async (): Promise<{ tools: Tool[] }> => {
server.setRequestHandler('tools/list', async (): Promise<{ tools: Tool[] }> => {
return {
tools: [
{
Expand Down Expand Up @@ -516,7 +507,7 @@ const createServer = (): Server => {
});

// Handle tool calls
server.setRequestHandler(CallToolRequestSchema, async (request, extra): Promise<CallToolResult | CreateTaskResult> => {
server.setRequestHandler('tools/call', async (request, extra): Promise<CallToolResult | CreateTaskResult> => {
const { name, arguments: args } = request.params;
const taskParams = (request.params._meta?.task || request.params.task) as { ttl?: number; pollInterval?: number } | undefined;

Expand Down Expand Up @@ -616,7 +607,7 @@ const createServer = (): Server => {
});

// Handle tasks/get
server.setRequestHandler(GetTaskRequestSchema, async (request): Promise<GetTaskResult> => {
server.setRequestHandler('tasks/get', async (request): Promise<GetTaskResult> => {
const { taskId } = request.params;
const task = await taskStore.getTask(taskId);
if (!task) {
Expand All @@ -626,7 +617,7 @@ const createServer = (): Server => {
});

// Handle tasks/result
server.setRequestHandler(GetTaskPayloadRequestSchema, async (request, extra): Promise<GetTaskPayloadResult> => {
server.setRequestHandler('tasks/result', async (request, extra): Promise<GetTaskPayloadResult> => {
const { taskId } = request.params;
console.log(`[Server] tasks/result called for task ${taskId}`);
return taskResultHandler.handle(taskId, server, extra.sessionId ?? '');
Expand Down
Loading
Loading