fix: support exactOptionalPropertyTypes in Transport interface#1736
Open
naman10parikh wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: support exactOptionalPropertyTypes in Transport interface#1736naman10parikh wants to merge 1 commit intomodelcontextprotocol:mainfrom
naman10parikh wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
Adds explicit `| undefined` to all optional callback properties on the Transport interface and all implementing classes. This allows consumers with `exactOptionalPropertyTypes: true` in their tsconfig.json to use the SDK without type errors. Without this fix, class properties initialized as `undefined` are incompatible with the interface under exactOptionalPropertyTypes, since `T | undefined` is not assignable to `T` when the property is present. Fixes modelcontextprotocol#1314
|
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
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.
Summary
Adds explicit
| undefinedto all optional callback properties on theTransportinterface and all implementing classes/test mocks. This allows consumers withexactOptionalPropertyTypes: truein theirtsconfig.jsonto use the SDK without type errors.Problem
When a consumer enables
exactOptionalPropertyTypes(a strict TypeScript config), class properties declared asonclose?: () => voidare initialized toundefinedby default. UnderexactOptionalPropertyTypes, this means the property type becomes(() => void) | undefined, which is not assignable to() => void— causing TS2420 errors:Solution
Add
| undefinedto all optional properties in theTransportinterface and all implementing classes:This is fully backwards-compatible — existing code that doesn't use
exactOptionalPropertyTypesis unaffected.Files Changed
packages/core/src/shared/transport.ts— Transport interface (6 properties)packages/client/src/client/sse.ts— SSEClientTransportpackages/client/src/client/streamableHttp.ts— StreamableHTTPClientTransportpackages/client/src/client/websocket.ts— WebSocketClientTransportpackages/client/src/client/stdio.ts— StdioClientTransportpackages/core/src/util/inMemory.ts— InMemoryTransportpackages/server/src/server/stdio.ts— StdioServerTransportpackages/server/src/server/streamableHttp.ts— WebStandardStreamableHTTPServerTransportpackages/core/test/shared/protocol.test.ts— MockTransportpackages/core/test/shared/protocolTransportHandling.test.ts— MockTransportTesting
pnpm build:allpasses with 0 errorsFixes #1314