Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-mcp-streams.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'incur': patch
---

Prevented completed MCP JSON responses from retaining transport stream state.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
],
"dependencies": {
"@cfworker/json-schema": "^4.1.1",
"@modelcontextprotocol/server": "^2.0.0-alpha.2",
"@modelcontextprotocol/server": "2.0.0-alpha.4",
"@scalar/openapi-types": "^0.8.0",
"@toon-format/toon": "^2.1.0",
"tokenx": "^1.3.0",
Expand Down
17 changes: 5 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions src/Cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Cli, Errors, Mcp, z } from 'incur'
import { execFile } from 'node:child_process'
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import { homedir, tmpdir } from 'node:os'
import { join } from 'node:path'
Expand Down Expand Up @@ -57,6 +58,20 @@ function mockMcpServeResponses(responses: unknown[]) {
})
}

function countRetainedMcpResponses() {
return new Promise<number>((resolve, reject) => {
execFile(
process.execPath,
['--expose-gc', '--import', 'tsx', 'test/fixtures/mcp-memory.ts'],
{ cwd: join(import.meta.dirname, '..'), timeout: 30_000 },
(error, stdout, stderr) => {
if (error) reject(new Error(stderr.trim() || stdout.trim() || error.message))
else resolve(Number(stdout.trim()))
},
)
})
}

function createConfigCli(flag?: string) {
const project = Cli.create('project').command('list', {
options: z.object({
Expand Down Expand Up @@ -5788,6 +5803,10 @@ describe('fetch', () => {
`)
})

test('completed JSON responses are released', async () => {
expect(await countRetainedMcpResponses()).toBe(0)
})

test('POST /mcp with tools/list → returns registered tools', async () => {
const cli = mcpCli()
const { sessionId } = await initSession(cli)
Expand Down
6 changes: 0 additions & 6 deletions src/McpSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,6 @@ describe('remote MCP command sources', () => {
"result": {
"tools": [
{
"execution": {
"taskSupport": "forbidden",
},
"inputSchema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
Expand All @@ -179,9 +176,6 @@ describe('remote MCP command sources', () => {
},
{
"description": "Search docs",
"execution": {
"taskSupport": "forbidden",
},
"inputSchema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
Expand Down
50 changes: 50 additions & 0 deletions test/fixtures/mcp-memory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Cli } from '../../src/index.js'

const cli = Cli.create('memory-test', {
mcp: { tools: { discovery: 'direct' } },
version: '1.0.0',
}).command('ping', { run: () => ({ pong: true }) })

let id = 0

function request(method: string, params: Record<string, unknown> = {}) {
return cli.fetch(
new Request('http://localhost/mcp', {
body: JSON.stringify({ id: ++id, jsonrpc: '2.0', method, params }),
headers: {
accept: 'application/json, text/event-stream',
'content-type': 'application/json',
},
method: 'POST',
}),
)
}

async function consume(method: string, params: Record<string, unknown> = {}) {
const response = await request(method, params)
await response.text()
}

async function weakResponse() {
const response = await request('tools/list')
await response.text()
return new WeakRef(response)
}

await consume('initialize', {
capabilities: {},
clientInfo: { name: 'memory-test', version: '1.0.0' },
protocolVersion: '2025-03-26',
})

const responses: WeakRef<Response>[] = []
for (let index = 0; index < 100; index++) responses.push(await weakResponse())

const gc = globalThis.gc
if (!gc) throw new Error('garbage collection is unavailable')
for (let index = 0; index < 5; index++) {
await new Promise(setImmediate)
gc()
}

console.log(responses.filter((response) => response.deref()).length)
Loading