File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import { mkdtemp, rm } from 'node:fs/promises';
1111import { tmpdir } from 'node:os' ;
1212import { join } from 'node:path' ;
1313import {
14+ capMcpOutput ,
1415 connectAllMcpServers ,
1516 connectMcpServer ,
1617 expandMcpResourceRefs ,
@@ -442,6 +443,19 @@ describe('parseHelperOutput', () => {
442443 } ) ;
443444} ) ;
444445
446+ describe ( 'capMcpOutput' , ( ) => {
447+ it ( 'passes through output under the cap' , ( ) => {
448+ expect ( capMcpOutput ( 'short' , 100 ) ) . toBe ( 'short' ) ;
449+ } ) ;
450+ it ( 'truncates over-long output with a notice' , ( ) => {
451+ const big = 'x' . repeat ( 120 ) ;
452+ const out = capMcpOutput ( big , 100 ) ;
453+ expect ( out . startsWith ( 'x' . repeat ( 100 ) ) ) . toBe ( true ) ;
454+ expect ( out ) . toMatch ( / 2 0 c h a r a c t e r s t r u n c a t e d / ) ;
455+ expect ( out ) . toMatch ( / 1 0 0 - c h a r c a p / ) ;
456+ } ) ;
457+ } ) ;
458+
445459describe ( 'parseResourceRefs' , ( ) => {
446460 it ( 'finds @server:scheme://path references' , ( ) => {
447461 const refs = parseResourceRefs (
Original file line number Diff line number Diff line change @@ -179,6 +179,20 @@ interface FinishableTransport extends Transport {
179179 finishAuth ( code : string ) : Promise < void > ;
180180}
181181
182+ /** Max characters of MCP tool output fed back to the model (keeps a runaway
183+ * server response from blowing the context window). */
184+ export const MCP_OUTPUT_CAP = 50_000 ;
185+
186+ /** Truncate over-long MCP output with a visible notice. Exported for testing. */
187+ export function capMcpOutput ( text : string , cap = MCP_OUTPUT_CAP ) : string {
188+ if ( text . length <= cap ) return text ;
189+ const omitted = text . length - cap ;
190+ return (
191+ text . slice ( 0 , cap ) +
192+ `\n\n[… ${ omitted } characters truncated — MCP output exceeded the ${ cap } -char cap]`
193+ ) ;
194+ }
195+
182196/**
183197 * Connect to one MCP server (stdio / http / sse). Returns a handle containing
184198 * the registered tools (qualified as `mcp__<server>__<tool>`).
@@ -264,7 +278,7 @@ export async function connectMcpServer(
264278 . map ( ( c ) => c . text ?? '' )
265279 . join ( '\n' ) || '' ;
266280 return {
267- content : textParts || '(MCP tool returned no text content)' ,
281+ content : textParts ? capMcpOutput ( textParts ) : '(MCP tool returned no text content)' ,
268282 isError : result . isError === true ,
269283 data : { serverName, serverToolName : t . name } ,
270284 } ;
You can’t perform that action at this time.
0 commit comments