Commit d82c518
committed
fix(appkit): mcp client hardening + reload() race
Address four agentic-review findings on PR #306. All four were P1 risks
under the MCP path or the markdown agent reload flow.
1. mcp callTool joined undefined text into 'undefined' literal
McpToolCallResult.content[].text is optional per the spec; the old
filter only matched `type === "text"`, so an entry like
{ type: "text" } (text undefined) flowed through and
`Array.join('\n')` emitted the literal string "undefined" — visible
to the agent and to the user.
Tighten the filter on both the success and error paths with a type
predicate that narrows on `typeof c.text === "string"`. The error
path falls back to a generic "MCP tool call failed" message when no
text entries survive.
2. mcp sendNotification ignored HTTP error status
`notifications/initialized` and other fire-and-forget notifications
completed `connect()` even when the server returned 4xx/5xx — a
silent connect followed by mysterious tool-call failures. MCP spec
says notifications don't acknowledge, so we keep the fire-and-forget
contract but log a warning when the server clearly rejected the
request. Wrap the fetch in try/catch so network failures surface in
the logs without breaking the protocol.
3. mcp response body had no size cap
`response.text()` and `response.json()` both buffer the entire body
into memory. A misconfigured or malicious MCP server could stream
unbounded data to exhaust client memory.
New `readResponseTextCapped()` helper streams the body via the
ReadableStream API and aborts once cumulative bytes cross
MCP_RESPONSE_BODY_LIMIT_BYTES (1 MiB — far above any normal
`initialize` / `tools/list` / `tools/call` JSON-RPC response). Used
on both the SSE and plain-JSON branches in sendRpc.
4. agents.reload() closed the live mcpClient mid-stream
Tool dispatch reads `this.mcpClient` at call time, not via capture.
The old `reload()` called `await this.mcpClient.close()` and
nulled the field; any in-flight stream that resolved the field
afterwards crashed with "MCP client is closed" mid-conversation.
Drop the synchronous close. The client owns only short-lived
fetch handles, so a reload doesn't need to tear down state — new
endpoints append to the existing connection map via
`connectHostedTools`, stale connections from removed configs
become unreachable through the new agent tool indexes (small
memory cost, no correctness hazard). The shutdown path still
closes — that's process teardown, where in-flight streams have
already been aborted via `abortActiveOperations`.
Tests: 4 new MCP cases (undefined-text filter on success + error
paths, generic error fallback, 1 MiB body cap), 1 reload-doesn't-close
regression, plus a connect-still-succeeds-on-4xx-notification case.
2298 tests passing across the workspace.
Signed-off-by: MarioCadenas <MarioCadenas@users.noreply.github.com>1 parent 63f8cce commit d82c518
4 files changed
Lines changed: 323 additions & 21 deletions
File tree
- packages/appkit/src
- connectors/mcp
- tests
- plugins/agents
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
37 | 83 | | |
38 | 84 | | |
39 | 85 | | |
| |||
261 | 307 | | |
262 | 308 | | |
263 | 309 | | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
264 | 320 | | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
| 321 | + | |
269 | 322 | | |
270 | 323 | | |
271 | 324 | | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
| 325 | + | |
276 | 326 | | |
277 | 327 | | |
278 | 328 | | |
| |||
334 | 384 | | |
335 | 385 | | |
336 | 386 | | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
337 | 397 | | |
338 | 398 | | |
339 | 399 | | |
340 | | - | |
341 | | - | |
| 400 | + | |
342 | 401 | | |
343 | 402 | | |
344 | 403 | | |
| |||
348 | 407 | | |
349 | 408 | | |
350 | 409 | | |
351 | | - | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
352 | 414 | | |
353 | 415 | | |
354 | 416 | | |
| |||
380 | 442 | | |
381 | 443 | | |
382 | 444 | | |
383 | | - | |
384 | | - | |
385 | | - | |
386 | | - | |
387 | | - | |
388 | | - | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
389 | 475 | | |
390 | 476 | | |
391 | 477 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
400 | 400 | | |
401 | 401 | | |
402 | 402 | | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
259 | 259 | | |
260 | 260 | | |
261 | 261 | | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
266 | 277 | | |
267 | 278 | | |
268 | 279 | | |
| |||
0 commit comments