Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

mcp-policy.spec.ts

116 lines3.4 KB

1import { test, expect } from './fixtures'
2import type { APIRequestContext } from '@playwright/test'
3​
4// The mock server marks only get_guitar_price read-only (fixtures/mcp/basic.json).
5​
6type StreamEvent = {
7 type: string
8 outcome?: {
9 type?: string
10 interrupts?: Array<{ metadata?: { kind?: string; toolName?: string } }>
11 }
13​
14function parseSse(body: string): Array<StreamEvent> {
15 return body
16 .split('\n')
17 .map((line) => line.trim())
18 .filter((line) => line.startsWith('data:'))
19 .flatMap((line) => {
20 try {
21 return [JSON.parse(line.slice('data:'.length).trim()) as StreamEvent]
22 } catch {
23 return []
24 }
25 })
27​
28test.describe('mcp-policy — client toolFilter / needsApproval', () => {
29 async function run(
30 request: APIRequestContext,
31 testId: string,
32 aimockPort: number,
33 policy: 'none' | 'readOnly' | 'approval',
34 ) {
35 const res = await request.post('/api/mcp-policy-test', {
36 headers: { 'Content-Type': 'application/json' },
37 data: {
38 threadId: `mcp-policy-thread-${testId}`,
39 runId: `mcp-policy-run-${testId}`,
40 state: {},
41 messages: [
42 {
43 id: 'mcp-policy-msg-1',
44 role: 'user',
45 content: '[mcp] how much is the strat guitar',
46 },
47 ],
48 tools: [],
49 context: [],
50 forwardedProps: { testId, aimockPort, policy },
51 },
52 })
53 const body = await res.text()
54 expect(res.ok(), `mcp-policy-test failed (${res.status()}): ${body}`).toBe(
55 true,
56 )
57 return parseSse(body)
58 }
59​
60 async function wireToolNames(
61 request: APIRequestContext,
62 testId: string,
63 aimockPort: number,
64 ) {
65 const journal = await request.get(
66 `http://127.0.0.1:${aimockPort}/v1/_requests`,
67 )
68 const entries = (await journal.json()) as Array<{
69 headers?: Record<string, string>
70 body?: { tools?: Array<{ function?: { name?: string } }> } | null
71 }>
72 const first = entries.find(
73 (entry) => entry.headers?.['x-test-id'] === testId,
74 )
75 return (first?.body?.tools ?? []).map((t) => t.function?.name)
76 }
77​
78 test('no policy sends every server tool', async ({
79 request,
80 testId,
81 aimockPort,
82 }) => {
83 await run(request, testId, aimockPort, 'none')
84 const names = await wireToolNames(request, testId, aimockPort)
85 expect(names).toContain('get_guitar_price')
86 expect(names).toContain('appraise_guitar_collection')
87 })
88​
89 test('toolFilter keeps only read-only tools on the wire', async ({
90 request,
91 testId,
92 aimockPort,
93 }) => {
94 const events = await run(request, testId, aimockPort, 'readOnly')
95 const names = await wireToolNames(request, testId, aimockPort)
96 expect(names).toEqual(['get_guitar_price'])
97 // The kept tool still runs.
98 expect(events.some((e) => e.type === 'TOOL_CALL_RESULT')).toBe(true)
99 expect(events.some((e) => e.type === 'RUN_ERROR')).toBe(false)
100 })
101​
102 test('needsApproval pauses the run before the tool executes', async ({
103 request,
104 testId,
105 aimockPort,
106 }) => {
107 const events = await run(request, testId, aimockPort, 'approval')
108 expect(events.some((e) => e.type === 'TOOL_CALL_RESULT')).toBe(false)
109 const finished = events.find((e) => e.type === 'RUN_FINISHED')
110 expect(finished?.outcome?.type).toBe('interrupt')
111 expect(finished?.outcome?.interrupts?.[0]?.metadata).toMatchObject({
112 kind: 'approval',
113 toolName: 'get_guitar_price',
114 })
115 })
116})