The HTTP API answers an unexpected failure with a generic message and leaves the details to the server log. The MCP tools returned any error's message to the client instead: a database, filesystem or upstream failure could hand an agent client connection strings, table names or internal paths. MCP tool results now follow the HTTP API's rules: domain errors keep their code, message and details, invalid input reports its validation issues, and anything else is INTERNAL_ERROR with a pointer to the server log, where the details are written. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
138 lines
6.2 KiB
TypeScript
138 lines
6.2 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
||
import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';
|
||
import type { Actor } from '@wishforge/contracts';
|
||
import { ControlPlane } from '../apps/api/src/domain/control-plane.js';
|
||
import { Services } from '../apps/api/src/http.js';
|
||
import { createMcpServer } from '../apps/api/src/mcp.js';
|
||
import { MemoryStore } from './helpers/memory-store.js';
|
||
|
||
const env = { NODE_ENV: 'test', WF_VAULT_KEY: 'a'.repeat(64), WF_PUBLIC_URL: 'https://console.example.test' };
|
||
const user: Actor = { tenantId: 'alpha', userId: 'owner', role: 'owner', kind: 'connector' };
|
||
let services: Services;
|
||
let client: Client;
|
||
const call = async (name: string, args: Record<string, unknown> = {}) => {
|
||
const response = (await client.callTool({ name, arguments: args })) as {
|
||
isError?: boolean;
|
||
content: Array<{ text: string }>;
|
||
};
|
||
const body = JSON.parse(response.content[0]!.text);
|
||
return response.isError ? { error: body.error } : body.data;
|
||
};
|
||
beforeEach(async () => {
|
||
const store = new MemoryStore();
|
||
store.seed('alpha');
|
||
services = new Services(new ControlPlane(store, env));
|
||
await services.control.createProject(
|
||
{ ...user, kind: 'human' },
|
||
{
|
||
name: 'Catalog service',
|
||
repositoryUrl: 'https://example.org/catalog.git',
|
||
verificationCommands: [['npm', 'test']],
|
||
},
|
||
);
|
||
await services.control.createProject(
|
||
{ ...user, kind: 'human' },
|
||
{ name: 'Billing', repositoryUrl: 'https://example.org/b.git' },
|
||
);
|
||
await services.connections.create(
|
||
{ ...user, kind: 'human' },
|
||
{ provider: 'codex', label: 'test', apiKey: 'fixture-key-only' },
|
||
);
|
||
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
||
await createMcpServer(services, user).connect(serverTransport);
|
||
client = new Client({ name: 'test-client', version: '1.0.0' });
|
||
await client.connect(clientTransport);
|
||
});
|
||
|
||
describe('MCP tools for agent clients (FR-A3, FR-A6, journey J1)', () => {
|
||
it('offers exactly the documented tools and no approval or configuration tools', async () => {
|
||
const { tools } = await client.listTools();
|
||
expect(tools.map(tool => tool.name).sort()).toEqual([
|
||
'answer_question',
|
||
'control_run',
|
||
'get_delivery',
|
||
'get_run',
|
||
'get_run_events',
|
||
'list_projects',
|
||
'request_release',
|
||
'submit_wish',
|
||
]);
|
||
});
|
||
it('finds a project without knowing its id, submits a wish and follows it', async () => {
|
||
const projects = await call('list_projects', { query: 'catalog' });
|
||
expect(projects).toHaveLength(1);
|
||
expect(projects[0]).toMatchObject({ name: 'Catalog service', verification_configured: true });
|
||
expect(projects[0].console_url).toBe(`https://console.example.test/projects/${projects[0].project_id}`);
|
||
|
||
const wish = {
|
||
project_id: projects[0].project_id,
|
||
idempotency_key: 'mcp-wish-0001',
|
||
goal: 'Add search to the catalog',
|
||
acceptanceCriteria: ['Searching an existing item returns its row'],
|
||
};
|
||
const submitted = await call('submit_wish', wish);
|
||
expect(submitted.console_url).toBe(`https://console.example.test/runs/${submitted.run_id}`);
|
||
// Retrying with the same key returns the same run.
|
||
expect((await call('submit_wish', wish)).run_id).toBe(submitted.run_id);
|
||
|
||
expect(await call('get_run', { run_id: submitted.run_id })).toMatchObject({ status: 'queued', stage: 'clarify' });
|
||
const events = await call('get_run_events', { run_id: submitted.run_id });
|
||
expect(events[0]).toMatchObject({ type: 'wish.submitted', seq: 1 });
|
||
expect(await call('get_run_events', { run_id: submitted.run_id, cursor: 1 })).toEqual([]);
|
||
|
||
const answered = await call('answer_question', {
|
||
run_id: submitted.run_id,
|
||
answer: 'Search must ignore case',
|
||
idempotency_key: 'mcp-answer-0001',
|
||
});
|
||
expect(answered.revision).toBe(2);
|
||
expect(
|
||
(await call('control_run', { run_id: submitted.run_id, action: 'pause', idempotency_key: 'mcp-pause-0001' }))
|
||
.status,
|
||
).toBe('paused');
|
||
const delivery = await call('get_delivery', { run_id: submitted.run_id });
|
||
expect(delivery).toMatchObject({ run_id: submitted.run_id, release: null, accepted: false, evidence: [] });
|
||
});
|
||
it('refuses a release before the run is verified and built', async () => {
|
||
const [project] = await call('list_projects', { query: 'catalog' });
|
||
const { run_id } = await call('submit_wish', {
|
||
project_id: project.project_id,
|
||
idempotency_key: 'mcp-wish-0002',
|
||
goal: 'Goal',
|
||
acceptanceCriteria: ['Criterion'],
|
||
});
|
||
expect(await call('request_release', { run_id, idempotency_key: 'mcp-release-0001' })).toEqual({
|
||
error: { code: 'RELEASE_NOT_READY', message: expect.any(String) },
|
||
});
|
||
});
|
||
it('keeps internal failures out of tool results, as the HTTP API does', async () => {
|
||
// An unexpected failure, such as the database's, carries details meant for the server log only.
|
||
services.control.projects = async () => {
|
||
throw new Error('relation "entities" does not exist (postgres://wishforge_app@127.0.0.1:55432)');
|
||
};
|
||
const logged = vi.spyOn(console, 'error').mockImplementation(() => undefined);
|
||
try {
|
||
const failed = await call('list_projects');
|
||
expect(failed).toEqual({
|
||
error: { code: 'INTERNAL_ERROR', message: 'Tool failed. Consult the server audit log.' },
|
||
});
|
||
expect(JSON.stringify(failed)).not.toContain('postgres');
|
||
expect(logged).toHaveBeenCalledWith(expect.stringContaining('relation "entities" does not exist'));
|
||
} finally {
|
||
logged.mockRestore();
|
||
}
|
||
});
|
||
it('acts only within the user’s own permissions', async () => {
|
||
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
||
const viewer: Actor = { tenantId: 'alpha', userId: 'viewer', role: 'viewer', kind: 'connector' };
|
||
await createMcpServer(services, viewer).connect(serverTransport);
|
||
const limited = new Client({ name: 'viewer-client', version: '1.0.0' });
|
||
await limited.connect(clientTransport);
|
||
const listed = (await limited.callTool({ name: 'list_projects', arguments: {} })) as {
|
||
content: Array<{ text: string }>;
|
||
};
|
||
expect(JSON.parse(listed.content[0]!.text).data).toEqual([]);
|
||
});
|
||
});
|