74 lines
3.4 KiB
JavaScript
74 lines
3.4 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import { createServer } from 'node:http'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { test } from 'node:test'
|
|
import { Client } from '@modelcontextprotocol/client'
|
|
import { StdioClientTransport } from '@modelcontextprotocol/client/stdio'
|
|
import { createMcpHandler, McpServer } from '@modelcontextprotocol/server'
|
|
import { toNodeHandler } from '@modelcontextprotocol/node'
|
|
|
|
test('stdio cancellation reaches the in-flight upstream logs follow', { timeout: 15000 }, async () => {
|
|
const entered = Promise.withResolvers()
|
|
const cancelled = Promise.withResolvers()
|
|
const finish = Promise.withResolvers()
|
|
const handler = createMcpHandler(() => {
|
|
const server = new McpServer({ name: 'owned-stdio-follow', version: '1.0.0' })
|
|
server.server.registerCapabilities({ tools: {} })
|
|
server.server.setRequestHandler('tools/call', async ({ params }, context) => {
|
|
assert.equal(params.name, 'mm_logs_follow')
|
|
const signal = context.mcpReq.signal
|
|
const abort = () => { cancelled.resolve(); finish.resolve() }
|
|
if (signal.aborted) abort()
|
|
else signal.addEventListener('abort', abort, { once: true })
|
|
entered.resolve()
|
|
try { await finish.promise } finally { signal.removeEventListener('abort', abort) }
|
|
return { content: [] }
|
|
})
|
|
return server
|
|
}, { legacy: 'stateless' })
|
|
const dispatch = toNodeHandler(handler)
|
|
const http = createServer((req, res) => {
|
|
res.once('close', () => {
|
|
if (req.method === 'POST' && !res.writableFinished) { cancelled.resolve(); finish.resolve() }
|
|
})
|
|
void dispatch(req, res)
|
|
})
|
|
const client = new Client({ name: 'owned-stdio-client', version: '1.0.0' })
|
|
const within = (promise, phase) => {
|
|
const signal = AbortSignal.timeout(5000)
|
|
return new Promise((resolve, reject) => {
|
|
const abort = () => reject(new Error(phase, { cause: signal.reason }))
|
|
signal.addEventListener('abort', abort, { once: true })
|
|
promise.then(resolve, reject).finally(() => signal.removeEventListener('abort', abort))
|
|
})
|
|
}
|
|
try {
|
|
await new Promise(resolve => http.listen(0, '127.0.0.1', resolve))
|
|
const transport = new StdioClientTransport({
|
|
command: process.execPath,
|
|
args: ['--import', import.meta.resolve('tsx'), fileURLToPath(new URL('../../src/scripts/mcp-stdio.ts', import.meta.url))],
|
|
env: { MCP_URL: `http://127.0.0.1:${http.address().port}/mcp`, MCP_TOKEN: 'synthetic-stdio-test' },
|
|
stderr: 'pipe',
|
|
})
|
|
const outbound = []
|
|
const send = transport.send.bind(transport)
|
|
transport.send = (message, options) => { outbound.push(message.method); return send(message, options) }
|
|
await within(client.connect(transport), 'stdio connection deadline')
|
|
const caller = new AbortController()
|
|
const call = client.callTool({ name: 'mm_logs_follow', arguments: {} }, { signal: caller.signal })
|
|
const rejected = assert.rejects(call)
|
|
void rejected.catch(() => {})
|
|
await within(entered.promise, 'upstream did not receive follow')
|
|
caller.abort()
|
|
assert(outbound.includes('notifications/cancelled'), JSON.stringify(outbound))
|
|
await within(Promise.all([cancelled.promise, rejected]), 'upstream did not observe cancellation')
|
|
await within(client.ping(), 'bridge did not remain usable')
|
|
} finally {
|
|
finish.resolve()
|
|
await client.close()
|
|
http.closeAllConnections()
|
|
await handler.close()
|
|
await new Promise(resolve => http.close(resolve))
|
|
}
|
|
})
|