Skip to main content

Matchers

The expect-mcp library provides custom Vitest matchers specifically designed for testing Model Context Protocol (MCP) integrations.

MCP Server Matchers

These matchers work with MCPStdinSubprocess instances to test server capabilities.

Tool Result Matchers

These matchers work with ToolCallResult instances returned by app.callTool().

Resource Result Matchers

These matchers work with ReadResourceResult instances returned by app.readResource().

Example

import { mcpShell } from 'expect-mcp';
import { test, expect } from 'vitest';

test('complete matcher example', async () => {
const app = mcpShell('node file-server.js');
await app.initialize();

// Server matchers
await expect(app).toHaveTool('read_file');
await expect(app).toHaveTools(['read_file', 'write_file']);
await expect(app).toHaveResource('config.json');

// Tool result matchers
const toolResult = await app.callTool('read_file', {
path: '/tmp/test.txt'
});
await expect(toolResult).toBeSuccessful();
await expect(toolResult).toHaveTextContent('Hello world');
await expect(toolResult).toMatchTextContent(/Hello \w+/);

// Resource result matchers
const resourceResult = await app.readResource('file:///app/config.json');
await expect(resourceResult).toHaveResourceContent('file:///app/config.json');
await expect(resourceResult).toHaveTextResource('{"key": "value"}');

await app.close();
});

See Also