fix: correct 6 bugs across multiple servers#3623
Open
wishhyt wants to merge 6 commits intomodelcontextprotocol:mainfrom
Open
fix: correct 6 bugs across multiple servers#3623wishhyt wants to merge 6 commits intomodelcontextprotocol:mainfrom
wishhyt wants to merge 6 commits intomodelcontextprotocol:mainfrom
Conversation
`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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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...inon Map never iterates entries (streamableHttp.ts)The SIGINT shutdown handler used
for...into iterate aMap<string, StreamableHTTPServerTransport>. SinceMapentries 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...ofwhich 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 barecatchon 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 bothstartsWithchecks.4. MEDIUM: Incorrect idempotentHint on sequential thinking tool (index.ts)
The tool was marked
idempotentHint: true, but it appends tothoughtHistoryon every call, making repeated identical calls produce differentthoughtHistoryLengthvalues and duplicate entries. Clients relying on this hint for auto-retry could corrupt the thinking chain.Fix: Changed
idempotentHinttofalse.5. MEDIUM: Memory migration rename error silently swallowed (memory/index.ts)
The nested try/catch structure meant that if
fs.rename()failed duringmemory.json → memory.jsonlmigration (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
colorandpetTypefields 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, andlegacyTitledEnum.Fix: Removed dead field references and added handlers for all schema-defined fields.
Server Details
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
Checklist
Additional context
All 6 commits are independent and can be cherry-picked individually if needed.