-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: MCP title generation fails — reconnect bridge + pre-parse body (#900) #908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
5f55adc
bee3646
386cc71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,18 +43,39 @@ async function main() { | |
| } | ||
|
|
||
| let httpClient: Client | null = null; | ||
| let connectPromise: Promise<Client> | null = null; | ||
|
|
||
| async function ensureHttpClient(): Promise<Client> { | ||
| if (httpClient) return httpClient; | ||
| const client = new Client( | ||
| { name: 'happy-stdio-bridge', version: '1.0.0' }, | ||
| { capabilities: {} } | ||
| ); | ||
| if (connectPromise) return connectPromise; | ||
|
|
||
| connectPromise = (async () => { | ||
| try { | ||
| const client = new Client( | ||
| { name: 'happy-stdio-bridge', version: '1.0.0' }, | ||
| { capabilities: {} } | ||
| ); | ||
| const transport = new StreamableHTTPClientTransport(new URL(baseUrl)); | ||
| transport.onclose = () => { | ||
| // Reset client on transport close so next call reconnects | ||
| if (httpClient === client) { | ||
| httpClient = null; | ||
| } | ||
| }; | ||
| await client.connect(transport); | ||
| client.onclose = () => { | ||
|
Comment on lines
+52
to
+65
|
||
| if (httpClient === client) { | ||
| httpClient = null; | ||
| } | ||
| }; | ||
| httpClient = client; | ||
| return client; | ||
| } finally { | ||
| connectPromise = null; | ||
| } | ||
| })(); | ||
|
|
||
| const transport = new StreamableHTTPClientTransport(new URL(baseUrl)); | ||
| await client.connect(transport); | ||
| httpClient = client; | ||
| return client; | ||
| return connectPromise; | ||
| } | ||
|
|
||
| // Create STDIO MCP server | ||
|
|
@@ -77,9 +98,12 @@ async function main() { | |
| try { | ||
| const client = await ensureHttpClient(); | ||
| const response = await client.callTool({ name: 'change_title', arguments: args }); | ||
| // Pass-through response from HTTP server | ||
| return response as any; | ||
| } catch (error) { | ||
| // Clean up stale client so the next invocation reconnects | ||
| const staleClient = httpClient; | ||
| httpClient = null; | ||
| try { await staleClient?.close(); } catch { /* ignore close errors */ } | ||
| return { | ||
| content: [ | ||
| { type: 'text', text: `Failed to change chat title: ${error instanceof Error ? error.message : String(error)}` }, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The request body is fully buffered into memory with no size limit before parsing. Even though the server binds to 127.0.0.1, a local client can still send an arbitrarily large POST and cause high memory usage. Add a maximum body size (and return 413 / abort reading) to prevent accidental or malicious oversized requests from impacting the CLI process.