v2 switch client examples to .callTool usage (#1628)

This commit is contained in:
Konstantin Konstantinov 2026-03-05 11:25:06 +02:00 • committed by GitHub
commit 4663a26f2a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 51 additions and 109 deletions

View file

@ -1,6 +1,6 @@
//#region prelude
import Anthropic from '@anthropic-ai/sdk';
import { Client, StdioClientTransport, type CallToolResult } from '@modelcontextprotocol/client';
import { Client, StdioClientTransport } from '@modelcontextprotocol/client';
import readline from 'readline/promises';
const ANTHROPIC_MODEL = 'claude-sonnet-4-5';
@ -84,7 +84,7 @@ class MCPClient {
const result = await this.mcp.callTool({
name: toolName,
arguments: toolArgs,
}) as CallToolResult;
});
finalText.push(`[Calling tool ${toolName} with args ${JSON.stringify(toolArgs)}]`);

View file

@ -9,8 +9,6 @@ import { createServer } from 'node:http';
import { createInterface } from 'node:readline';
import type {
CallToolRequest,
CallToolResult,
ElicitRequest,
ElicitRequestURLParams,
ElicitResult,
@ -687,16 +685,8 @@ async function callTool(name: string, args: Record<string, unknown>): Promise<vo
}
try {
const request: CallToolRequest = {
method: 'tools/call',
params: {
name,
arguments: args
}
};
console.log(`Calling tool '${name}' with args:`, args);
const result = (await client.request(request)) as CallToolResult;
const result = await client.callTool({ name, arguments: args });
console.log('Tool result:');
const resourceLinks: ResourceLink[] = [];

View file

@ -1,4 +1,4 @@
import type { CallToolRequest, CallToolResult } from '@modelcontextprotocol/client';
import type { CallToolResult } from '@modelcontextprotocol/client';
import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client';
/**
@ -48,19 +48,14 @@ async function createAndRunClient(config: ClientConfig): Promise<{ id: string; r
// Call the specified tool
console.log(`[${config.id}] Calling tool: ${config.toolName}`);
const toolRequest: CallToolRequest = {
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.callTool({
name: config.toolName,
arguments: {
...config.toolArguments,
// Add client ID to arguments for identification in notifications
caller: config.id
}
};
const result = (await client.request(toolRequest)) as CallToolResult;
});
console.log(`[${config.id}] Tool call completed`);
// Keep the connection open for a bit to receive notifications

View file

@ -1,4 +1,4 @@
import type { CallToolRequest, CallToolResult, ListToolsRequest } from '@modelcontextprotocol/client';
import type { CallToolResult, ListToolsRequest } from '@modelcontextprotocol/client';
import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client';
/**
@ -108,47 +108,29 @@ async function listTools(client: Client): Promise<void> {
async function startParallelNotificationTools(client: Client): Promise<Record<string, CallToolResult>> {
try {
// Define multiple tool calls with different configurations
const toolCalls: Array<{ caller: string; request: CallToolRequest }> = [
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
}
}
args: {
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
}
}
args: {
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
}
}
args: {
interval: 1, // 0.1 second between notifications
count: 3, // Send just 3 notifications
caller: 'burst-notifier' // Identify this tool call
}
}
];
@ -156,10 +138,10 @@ async function startParallelNotificationTools(client: Client): Promise<Record<st
console.log(`Starting ${toolCalls.length} notification tools in parallel...`);
// Start all tool calls in parallel
const toolPromises = toolCalls.map(({ caller, request }) => {
const toolPromises = toolCalls.map(({ caller, args }) => {
console.log(`Starting tool call for ${caller}...`);
return client
.request(request)
.callTool({ name: 'start-notification-stream', arguments: args })
.then(result => ({ caller, result }))
.catch(error => {
console.error(`Error in tool call for ${caller}:`, error);
@ -173,7 +155,7 @@ async function startParallelNotificationTools(client: Client): Promise<Record<st
// Organize results by caller
const resultsByTool: Record<string, CallToolResult> = {};
for (const { caller, result } of results) {
resultsByTool[caller] = result as CallToolResult;
resultsByTool[caller] = result;
}
return resultsByTool;

View file

@ -4,7 +4,7 @@ import { createServer } from 'node:http';
import { createInterface } from 'node:readline';
import { URL } from 'node:url';
import type { CallToolRequest, CallToolResult, ListToolsRequest, OAuthClientMetadata } from '@modelcontextprotocol/client';
import type { CallToolResult, ListToolsRequest, OAuthClientMetadata } from '@modelcontextprotocol/client';
import { Client, StreamableHTTPClientTransport, UnauthorizedError } from '@modelcontextprotocol/client';
import open from 'open';
@ -306,15 +306,10 @@ class InteractiveOAuthClient {
}
try {
const request: CallToolRequest = {
method: 'tools/call',
params: {
name: toolName,
arguments: toolArgs
}
};
const result = (await this.client.request(request)) as CallToolResult;
const result = await this.client.callTool({
name: toolName,
arguments: toolArgs
});
console.log(`\n🔧 Tool '${toolName}' result:`);
if (result.content) {

View file

@ -1,7 +1,6 @@
import { createInterface } from 'node:readline';
import type {
CallToolRequest,
CallToolResult,
GetPromptRequest,
ListPromptsRequest,
@ -646,16 +645,8 @@ async function callTool(name: string, args: Record<string, unknown>): Promise<vo
}
try {
const request: CallToolRequest = {
method: 'tools/call',
params: {
name,
arguments: args
}
};
console.log(`Calling tool '${name}' with args:`, args);
const result = (await client.request(request)) as CallToolResult;
const result = await client.callTool({ name, arguments: args });
console.log('Tool result:');
const resourceLinks: ResourceLink[] = [];
@ -746,23 +737,18 @@ async function runNotificationsToolWithResumability(interval: number, count: num
console.log(`Starting notification stream with resumability: interval=${interval}ms, count=${count || 'unlimited'}`);
console.log(`Using resumption token: ${notificationsToolLastEventId || 'none'}`);
const request: CallToolRequest = {
method: 'tools/call',
params: {
name: 'start-notification-stream',
arguments: { interval, count }
}
};
const onLastEventIdUpdate = (event: string) => {
notificationsToolLastEventId = event;
console.log(`Updated resumption token: ${event}`);
};
const result = (await client.request(request, {
resumptionToken: notificationsToolLastEventId,
onresumptiontoken: onLastEventIdUpdate
})) as CallToolResult;
const result = await client.callTool(
{ name: 'start-notification-stream', arguments: { interval, count } },
{
resumptionToken: notificationsToolLastEventId,
onresumptiontoken: onLastEventIdUpdate
}
);
console.log('Tool result:');
for (const item of result.content) {

View file

@ -1,4 +1,4 @@
import type { CallToolRequest, CallToolResult, ListToolsRequest } from '@modelcontextprotocol/client';
import type { ListToolsRequest } from '@modelcontextprotocol/client';
import { Client, SSEClientTransport, StreamableHTTPClientTransport } from '@modelcontextprotocol/client';
/**
@ -149,20 +149,14 @@ async function listTools(client: Client): Promise<void> {
*/
async function startNotificationTool(client: Client): Promise<void> {
try {
// Call the notification tool using reasonable defaults
const request: CallToolRequest = {
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)) as CallToolResult;
const result = await client.callTool({
name: 'start-notification-stream',
arguments: {
interval: 1000, // 1 second between notifications
count: 5 // Send 5 notifications
}
});
console.log('Tool result:');
for (const item of result.content) {