callTool
Calls a tool on the MCP server.
Will throw an error if:
- The server did not declare 'tools' in the capabilities section.
- The server did not name this tool in the tools list.
- The tool call fails.
Syntax
app.callTool(name: string, arguments?: any): Promise<ToolCallResult>
Parameters
name: The name of the tool to callarguments: Optional arguments to pass to the tool
Returns
A Promise that resolves to a ToolCallResult instance containing the tool's response.
Example
import { mcpShell } from 'expect-mcp';
import { test, expect } from 'vitest';
test('call a tool on the server', async () => {
const app = mcpShell('node file-server.js');
await app.initialize();
// Call the read_file tool
const result = await app.callTool('read_file', {
path: '/path/to/file.txt',
});
// result is a ToolCallResult instance
expect(result).toBeDefined();
expect(result.content).toBeDefined();
// Use helper methods
const text = result.getTextContent();
expect(text).toBeDefined();
// Or use matchers
await expect(result).toBeSuccessful();
await app.close();
});
Timeout Configuration
You can configure the timeout for tool calls using the requestTimeout option:
const app = mcpShell('node slow-server.js', {
requestTimeout: 30000, // 30 seconds
});
const result = await app.callTool('slow_operation', {});
See Also
- ToolCallResult - The class returned by callTool
- mcpShell - Create an MCP subprocess
- toHaveTool - Assert that a tool exists
- Matchers - Custom matchers for validating tool results