Skip to content

fix: correct 6 bugs across multiple servers#3623

Open
wishhyt wants to merge 6 commits intomodelcontextprotocol:mainfrom
wishhyt:fix/multiple-bug-fixes
Open

fix: correct 6 bugs across multiple servers#3623
wishhyt wants to merge 6 commits intomodelcontextprotocol:mainfrom
wishhyt:fix/multiple-bug-fixes

Conversation

@wishhyt
Copy link

@wishhyt wishhyt commented Mar 18, 2026

Description

This PR fixes 6 confirmed bugs found across multiple server implementations via systematic code audit. Each fix is a separate commit for ease of review.

Bug Fixes

1. CRITICAL: for...in on Map never iterates entries (streamableHttp.ts)

The SIGINT shutdown handler used for...in to iterate a Map<string, StreamableHTTPServerTransport>. Since Map entries are not enumerable own properties, the loop body never executed, meaning active transports were never closed on shutdown — causing resource leaks.

Fix: Changed to for...of which correctly iterates Map entries.

2. HIGH: Inner catch swallows access-denied security error (filesystem/lib.ts)

In validatePath, when a new file's parent directory exists but is outside allowed directories, the "Access denied" error thrown on line 131 was immediately caught by the bare catch on line 134 and replaced with a misleading "Parent directory does not exist" message.

Fix: The inner catch now re-throws errors that start with "Access denied".

3. MEDIUM: Always-false guard condition in parseResourceId (templates.ts)

The guard uri.startsWith(textUriBase) && uri.startsWith(blobUriBase) is always false since a URI cannot start with both "demo://resource/dynamic/text" and "demo://resource/dynamic/blob". The intended check was to reject URIs matching neither prefix.

Fix: Added ! negation to both startsWith checks.

4. MEDIUM: Incorrect idempotentHint on sequential thinking tool (index.ts)

The tool was marked idempotentHint: true, but it appends to thoughtHistory on every call, making repeated identical calls produce different thoughtHistoryLength values and duplicate entries. Clients relying on this hint for auto-retry could corrupt the thinking chain.

Fix: Changed idempotentHint to false.

5. MEDIUM: Memory migration rename error silently swallowed (memory/index.ts)

The nested try/catch structure meant that if fs.rename() failed during memory.json → memory.jsonl migration (e.g. permission error), the exception would bubble into the outer catch which silently returned the new path. The server would start with a non-existent file and overwrite the user's data on the first write.

Fix: Restructured to flat try/catch blocks so rename errors propagate to the caller.

6. MEDIUM: Elicitation response handler references non-existent schema fields (trigger-elicitation-request.ts)

The response handler referenced color and petType fields that were not defined in the elicitation request schema (thus always undefined), while omitting fields that are actually in the schema: firstLine, untitledSingleSelectEnum, untitledMultipleSelectEnum, titledSingleSelectEnum, titledMultipleSelectEnum, and legacyTitledEnum.

Fix: Removed dead field references and added handlers for all schema-defined fields.

Server Details

  • Server: everything, filesystem, memory, sequential-thinking
  • Changes to: tools, resources, transports, core logic

Motivation and Context

These bugs were found through systematic code audit. Each is a clear, unambiguous defect with a deterministic fix that doesn't alter intended semantics.

How Has This Been Tested?

Each fix was verified by reading the original code, confirming the bug exists, applying the minimal correct fix, and re-reading the modified code. The changes are minimal and localized.

Breaking Changes

None. All fixes correct existing behavior to match documented intent.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • I have read the MCP Protocol Documentation
  • My changes follows MCP security best practices
  • My code follows the repository's style guidelines
  • I have added appropriate error handling

Additional context

All 6 commits are independent and can be cherry-picked individually if needed.

wishhyt added 6 commits March 18, 2026 16:58
`for...in` iterates enumerable own properties, which a Map does not
expose for its entries. The SIGINT handler's cleanup loop therefore
never executed, leaking active transports on shutdown. Replaced with
`for...of` which correctly iterates Map entries.

Made-with: Cursor
The bare catch block on the ENOENT path for new files was catching all
errors, including the "Access denied" error thrown when the parent
directory resolves outside allowed directories. This replaced a
security-relevant error with a misleading "Parent directory does not
exist" message. Now the inner catch re-throws access-denied errors.

Made-with: Cursor
The condition used && between two startsWith checks for different URI
prefixes (text vs blob), which can never both be true for the same
URI. The intent is to reject URIs matching neither prefix. Changed to
negate both checks so the guard correctly throws for unknown URIs.

Made-with: Cursor
The tool appends to thoughtHistory on every invocation, so repeated
calls with identical arguments produce different results
(thoughtHistoryLength increments and duplicate entries appear). This
makes the tool non-idempotent, and clients relying on the hint for
automatic retries could corrupt the thinking chain.

Made-with: Cursor
The nested try/catch structure meant that if fs.rename() failed during
memory.json → memory.jsonl migration (e.g. permission error), the
exception would bubble into the outer catch which silently returned the
new path. The server would then start with a non-existent file and
overwrite the user's data on the first write. Restructured to separate
the access checks so rename failures propagate to the caller.

Made-with: Cursor
The response handler referenced 'color' and 'petType' fields that do
not exist in the elicitation request schema, so they would never have
values. Meanwhile, fields actually defined in the schema (firstLine,
untitledSingleSelectEnum, untitledMultipleSelectEnum,
titledSingleSelectEnum, titledMultipleSelectEnum, legacyTitledEnum)
were never displayed. Removed the dead references and added handlers
for all schema-defined fields.

Made-with: Cursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant