Skip to content

feat: Context API - simplified types (alternative to #1241)#1465

Closed
felixweinberger wants to merge 1 commit intomainfrom
fweinberger/ctx-simplify-pr
Closed

feat: Context API - simplified types (alternative to #1241)#1465
felixweinberger wants to merge 1 commit intomainfrom
fweinberger/ctx-simplify-pr

Conversation

@felixweinberger
Copy link
Contributor

Summary

Alternative implementation of the Context API from PR #1241, using plain types and POJOs instead of a class hierarchy.

This PR replaces the flat RequestHandlerExtra type with a structured context system using 4 types:

Type Description
BaseContext Common context for all handlers (session, mcpReq, http, task, notification)
ServerContext Extends BaseContext with server-specific methods (logging, elicitation, sampling, SSE controls)
ClientContext Type alias for BaseContext with client-specific generics
TaskContext Task-related fields (id, store, requestedTtl)

Key differences from #1241

Aspect PR #1241 This PR
Implementation Abstract class hierarchy (BaseContext class → ServerContext/ClientContext subclasses) Plain types with POJO construction
Type count ~13 types/interfaces/classes 4 types
Construction new ServerContext(...) / new ClientContext(...) Plain object literals with closures
Type checking ctx instanceof ServerContext Duck-type checks ("elicitInput" in ctx.mcpReq)
Method sharing Prototype-based (shared across instances) Closures per request (same as current RequestHandlerExtra)

Context structure (same nested grouping as #1241)

ctx.sessionId
ctx.mcpReq.id / .method / ._meta / .signal / .send()
ctx.http?.authInfo / .req / .closeSSE / .closeStandaloneSSE  // server only
ctx.task?.id / .store / .requestedTtl
ctx.notification.send() / .log() / .debug() / .info() / ...  // logging on server only
ctx.mcpReq.elicitInput() / .requestSampling()                // server only

Motivation

  • Simpler: 4 types vs ~13 — easier to understand and maintain
  • Consistent: Keeps the POJO+closure pattern from the existing RequestHandlerExtra
  • Same API surface: Users get the same nested structure and convenience methods
  • No runtime overhead: No class instantiation, prototype chains, or instanceof checks

Breaking Changes

Same breaking changes as #1241RequestHandlerExtra properties move to the nested context structure.

How Has This Been Tested

  • All existing tests updated and passing (497 tests)
  • pnpm check:all (typecheck + lint) passes
  • pnpm test:all passes

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

@changeset-bot
Copy link

changeset-bot bot commented Feb 3, 2026

🦋 Changeset detected

Latest commit: d9abbf4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 8 packages
Name Type
@modelcontextprotocol/express Patch
@modelcontextprotocol/hono Patch
@modelcontextprotocol/node Patch
@modelcontextprotocol/eslint-config Patch
@modelcontextprotocol/test-integration Patch
@modelcontextprotocol/client Patch
@modelcontextprotocol/server Patch
@modelcontextprotocol/core Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 3, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@1465

@modelcontextprotocol/server

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@1465

@modelcontextprotocol/express

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/express@1465

@modelcontextprotocol/hono

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/hono@1465

@modelcontextprotocol/node

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/node@1465

commit: d9abbf4

@felixweinberger felixweinberger force-pushed the fweinberger/ctx-simplify-pr branch 2 times, most recently from ab4c876 to b20a543 Compare February 3, 2026 13:25
/**
* Request-scoped TaskStore interface.
*/
export interface RequestTaskStore {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this just moved out of protocol into packages/core/src/experimental/requestTaskStore.ts

Replaces the flat RequestHandlerExtra type with a structured context system
using 4 plain types: BaseContext, ServerContext, ClientContext, TaskContext.

- Nested structure groups related fields (mcpReq, http, task, notification)
- Separate ServerContext/ClientContext types for server and client handlers
- Server-specific convenience methods (logging, sampling, elicitation)
- Plain object (POJO) construction with closures - no classes
- Type-safe contexts without runtime instanceof checks

Based on the design from PR #1241, simplified from class hierarchy to plain types.

BREAKING CHANGE: RequestHandlerExtra is replaced by BaseContext with nested structure.
See docs/migration.md for migration guide.
@felixweinberger felixweinberger force-pushed the fweinberger/ctx-simplify-pr branch from b20a543 to d9abbf4 Compare February 3, 2026 14:38
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