44 ElicitRequest ,
55 ElicitResult ,
66 ElicitResultSchema ,
7+ JSONRPCRequest ,
78 LoggingMessageNotification ,
89 Notification ,
910 Request ,
@@ -12,16 +13,15 @@ import {
1213 RequestMeta ,
1314 Result ,
1415 ServerNotification ,
15- ServerRequest ,
16- ServerResult
16+ ServerRequest
1717} from '../types.js' ;
1818import { RequestHandlerExtra , RequestOptions , RequestTaskStore } from '../shared/protocol.js' ;
1919import { Server } from './index.js' ;
20- import { RequestContext } from './requestContext.js' ;
2120import { AuthInfo } from './auth/types.js' ;
2221import { AnySchema , SchemaOutput } from './zod-compat.js' ;
2322
24- export interface ContextInterface < RequestT extends Request = Request , NotificationT extends Notification = Notification > extends RequestHandlerExtra < ServerRequest | RequestT , NotificationT | ServerNotification > {
23+ export interface ContextInterface < RequestT extends Request = Request , NotificationT extends Notification = Notification >
24+ extends RequestHandlerExtra < ServerRequest | RequestT , NotificationT | ServerNotification > {
2525 elicit ( params : ElicitRequest [ 'params' ] , options ?: RequestOptions ) : Promise < ElicitResult > ;
2626 requestSampling : ( params : CreateMessageRequest [ 'params' ] , options ?: RequestOptions ) => Promise < CreateMessageResult > ;
2727 log ( params : LoggingMessageNotification [ 'params' ] , sessionId ?: string ) : Promise < void > ;
@@ -35,32 +35,61 @@ export interface ContextInterface<RequestT extends Request = Request, Notificati
3535 *
3636 * Implements the RequestHandlerExtra interface for backwards compatibility.
3737 */
38- export class Context <
39- RequestT extends Request = Request ,
40- NotificationT extends Notification = Notification ,
41- ResultT extends Result = Result
42- > implements ContextInterface < RequestT , NotificationT >
38+ export class Context < RequestT extends Request = Request , NotificationT extends Notification = Notification , ResultT extends Result = Result >
39+ implements ContextInterface < RequestT , NotificationT >
4340{
4441 private readonly server : Server < RequestT , NotificationT , ResultT > ;
4542
4643 /**
4744 * The request context.
4845 * A type-safe context that is passed to request handlers.
4946 */
50- public readonly requestCtx : RequestContext <
51- RequestT | ServerRequest ,
52- NotificationT | ServerNotification ,
53- ResultT | ServerResult
54- > ;
47+ private readonly requestCtx : RequestHandlerExtra < ServerRequest | RequestT , ServerNotification | NotificationT > ;
48+
49+ /**
50+ * The MCP context - Contains information about the current MCP request and session.
51+ */
52+ public readonly mcpContext : {
53+ /**
54+ * The JSON-RPC ID of the request being handled.
55+ * This can be useful for tracking or logging purposes.
56+ */
57+ requestId : RequestId ;
58+ /**
59+ * The method of the request.
60+ */
61+ method : string ;
62+ /**
63+ * The metadata of the request.
64+ */
65+ _meta ?: RequestMeta ;
66+ /**
67+ * The session ID of the request.
68+ */
69+ sessionId ?: string ;
70+ } ;
5571
5672 constructor ( args : {
5773 server : Server < RequestT , NotificationT , ResultT > ;
58- requestCtx : RequestContext < RequestT | ServerRequest , NotificationT | ServerNotification , ResultT | ServerResult > ;
74+ request : JSONRPCRequest ;
75+ requestCtx : RequestHandlerExtra < ServerRequest | RequestT , ServerNotification | NotificationT > ;
5976 } ) {
6077 this . server = args . server ;
6178 this . requestCtx = args . requestCtx ;
79+ this . mcpContext = {
80+ requestId : args . requestCtx . requestId ,
81+ method : args . request . method ,
82+ _meta : args . requestCtx . _meta ,
83+ sessionId : args . requestCtx . sessionId
84+ } ;
6285 }
6386
87+ /**
88+ * The JSON-RPC ID of the request being handled.
89+ * This can be useful for tracking or logging purposes.
90+ *
91+ * @deprecated Use {@link mcpContext.requestId} instead.
92+ */
6493 public get requestId ( ) : RequestId {
6594 return this . requestCtx . requestId ;
6695 }
@@ -77,12 +106,18 @@ export class Context<
77106 return this . requestCtx . requestInfo ;
78107 }
79108
109+ /**
110+ * @deprecated Use {@link mcpContext._meta} instead.
111+ */
80112 public get _meta ( ) : RequestMeta | undefined {
81113 return this . requestCtx . _meta ;
82114 }
83115
116+ /**
117+ * @deprecated Use {@link mcpContext.sessionId} instead.
118+ */
84119 public get sessionId ( ) : string | undefined {
85- return this . requestCtx . sessionId ;
120+ return this . mcpContext . sessionId ;
86121 }
87122
88123 public get taskId ( ) : string | undefined {
@@ -97,12 +132,12 @@ export class Context<
97132 return this . requestCtx . taskRequestedTtl ?? undefined ;
98133 }
99134
100- public closeSSEStream = ( ) : void => {
101- return this . requestCtx ? .closeSSEStream ( ) ;
135+ public get closeSSEStream ( ) : ( ( ) => void ) | undefined {
136+ return this . requestCtx . closeSSEStream ;
102137 }
103138
104- public closeStandaloneSSEStream = ( ) : void => {
105- return this . requestCtx ? .closeStandaloneSSEStream ( ) ;
139+ public get closeStandaloneSSEStream ( ) : ( ( ) => void ) | undefined {
140+ return this . requestCtx . closeStandaloneSSEStream ;
106141 }
107142
108143 /**
@@ -111,7 +146,7 @@ export class Context<
111146 * This is used by certain transports to correctly associate related messages.
112147 */
113148 public sendNotification = ( notification : NotificationT | ServerNotification ) : Promise < void > => {
114- return this . requestCtx . sendNotification ( notification ) ;
149+ return this . server . notification ( notification ) ;
115150 } ;
116151
117152 /**
@@ -124,7 +159,7 @@ export class Context<
124159 resultSchema : U ,
125160 options ?: RequestOptions
126161 ) : Promise < SchemaOutput < U > > => {
127- return this . requestCtx . sendRequest ( request , resultSchema , { ...options , relatedRequestId : this . requestId } ) ;
162+ return this . server . request ( request , resultSchema , { ...options , relatedRequestId : this . requestId } ) ;
128163 } ;
129164
130165 /**
@@ -219,4 +254,4 @@ export class Context<
219254 sessionId
220255 ) ;
221256 }
222- }
257+ }
0 commit comments