-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Context API in callbacks #1241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KKonstantinov
wants to merge
9
commits into
modelcontextprotocol:main
Choose a base branch
from
KKonstantinov:feature/ctx-in-callbacks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+765
−65
Open
Context API in callbacks #1241
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
107767a
save commit
KKonstantinov 68ff665
context API - backwards compatible introduction
KKonstantinov f58b491
fixes
KKonstantinov 459bff4
Merge branch 'main' into feature/ctx-in-callbacks
KKonstantinov a23e2f2
Merge branch 'main' into feature/ctx-in-callbacks
KKonstantinov 4ff84a1
Merge branch 'main' into feature/ctx-in-callbacks
KKonstantinov e89d9d4
moved properties under objects
KKonstantinov 187a3cd
prettier fix
KKonstantinov 96169b3
move logger methods under loggingNotification
KKonstantinov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,257 @@ | ||
| import { | ||
| CreateMessageRequest, | ||
| CreateMessageResult, | ||
| ElicitRequest, | ||
| ElicitResult, | ||
| ElicitResultSchema, | ||
| JSONRPCRequest, | ||
| LoggingMessageNotification, | ||
| Notification, | ||
| Request, | ||
| RequestId, | ||
| RequestInfo, | ||
| RequestMeta, | ||
| Result, | ||
| ServerNotification, | ||
| ServerRequest | ||
| } from '../types.js'; | ||
| import { RequestHandlerExtra, RequestOptions, RequestTaskStore } from '../shared/protocol.js'; | ||
| import { Server } from './index.js'; | ||
| import { AuthInfo } from './auth/types.js'; | ||
| import { AnySchema, SchemaOutput } from './zod-compat.js'; | ||
|
|
||
| export interface ContextInterface<RequestT extends Request = Request, NotificationT extends Notification = Notification> | ||
| extends RequestHandlerExtra<ServerRequest | RequestT, NotificationT | ServerNotification> { | ||
| elicit(params: ElicitRequest['params'], options?: RequestOptions): Promise<ElicitResult>; | ||
| requestSampling: (params: CreateMessageRequest['params'], options?: RequestOptions) => Promise<CreateMessageResult>; | ||
| log(params: LoggingMessageNotification['params'], sessionId?: string): Promise<void>; | ||
| debug(message: string, extraLogData?: Record<string, unknown>, sessionId?: string): Promise<void>; | ||
| info(message: string, extraLogData?: Record<string, unknown>, sessionId?: string): Promise<void>; | ||
| warning(message: string, extraLogData?: Record<string, unknown>, sessionId?: string): Promise<void>; | ||
| error(message: string, extraLogData?: Record<string, unknown>, sessionId?: string): Promise<void>; | ||
KKonstantinov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| /** | ||
| * A context object that is passed to request handlers. | ||
| * | ||
| * Implements the RequestHandlerExtra interface for backwards compatibility. | ||
| */ | ||
| export class Context<RequestT extends Request = Request, NotificationT extends Notification = Notification, ResultT extends Result = Result> | ||
| implements ContextInterface<RequestT, NotificationT> | ||
| { | ||
| private readonly server: Server<RequestT, NotificationT, ResultT>; | ||
|
|
||
| /** | ||
| * The request context. | ||
| * A type-safe context that is passed to request handlers. | ||
| */ | ||
| private readonly requestCtx: RequestHandlerExtra<ServerRequest | RequestT, ServerNotification | NotificationT>; | ||
|
|
||
| /** | ||
| * The MCP context - Contains information about the current MCP request and session. | ||
| */ | ||
| public readonly mcpContext: { | ||
| /** | ||
| * The JSON-RPC ID of the request being handled. | ||
| * This can be useful for tracking or logging purposes. | ||
| */ | ||
| requestId: RequestId; | ||
| /** | ||
| * The method of the request. | ||
| */ | ||
| method: string; | ||
| /** | ||
| * The metadata of the request. | ||
| */ | ||
| _meta?: RequestMeta; | ||
| /** | ||
| * The session ID of the request. | ||
| */ | ||
| sessionId?: string; | ||
| }; | ||
|
|
||
| constructor(args: { | ||
| server: Server<RequestT, NotificationT, ResultT>; | ||
| request: JSONRPCRequest; | ||
| requestCtx: RequestHandlerExtra<ServerRequest | RequestT, ServerNotification | NotificationT>; | ||
| }) { | ||
| this.server = args.server; | ||
| this.requestCtx = args.requestCtx; | ||
| this.mcpContext = { | ||
| requestId: args.requestCtx.requestId, | ||
| method: args.request.method, | ||
| _meta: args.requestCtx._meta, | ||
| sessionId: args.requestCtx.sessionId | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * The JSON-RPC ID of the request being handled. | ||
| * This can be useful for tracking or logging purposes. | ||
| * | ||
| * @deprecated Use {@link mcpContext.requestId} instead. | ||
| */ | ||
| public get requestId(): RequestId { | ||
| return this.requestCtx.requestId; | ||
| } | ||
|
|
||
| public get signal(): AbortSignal { | ||
| return this.requestCtx.signal; | ||
| } | ||
|
|
||
| public get authInfo(): AuthInfo | undefined { | ||
| return this.requestCtx.authInfo; | ||
| } | ||
|
|
||
| public get requestInfo(): RequestInfo | undefined { | ||
| return this.requestCtx.requestInfo; | ||
| } | ||
|
|
||
| /** | ||
| * @deprecated Use {@link mcpContext._meta} instead. | ||
| */ | ||
| public get _meta(): RequestMeta | undefined { | ||
| return this.requestCtx._meta; | ||
| } | ||
|
|
||
| /** | ||
| * @deprecated Use {@link mcpContext.sessionId} instead. | ||
| */ | ||
| public get sessionId(): string | undefined { | ||
| return this.mcpContext.sessionId; | ||
| } | ||
|
|
||
| public get taskId(): string | undefined { | ||
| return this.requestCtx.taskId; | ||
| } | ||
|
|
||
| public get taskStore(): RequestTaskStore | undefined { | ||
| return this.requestCtx.taskStore; | ||
| } | ||
|
|
||
| public get taskRequestedTtl(): number | undefined { | ||
| return this.requestCtx.taskRequestedTtl ?? undefined; | ||
| } | ||
KKonstantinov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public get closeSSEStream(): (() => void) | undefined { | ||
KKonstantinov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return this.requestCtx.closeSSEStream; | ||
| } | ||
|
|
||
| public get closeStandaloneSSEStream(): (() => void) | undefined { | ||
| return this.requestCtx.closeStandaloneSSEStream; | ||
| } | ||
|
|
||
| /** | ||
| * Sends a notification that relates to the current request being handled. | ||
| * | ||
| * This is used by certain transports to correctly associate related messages. | ||
| */ | ||
| public sendNotification = (notification: NotificationT | ServerNotification): Promise<void> => { | ||
| return this.requestCtx.sendNotification(notification); | ||
| }; | ||
|
|
||
| /** | ||
| * Sends a request that relates to the current request being handled. | ||
| * | ||
| * This is used by certain transports to correctly associate related messages. | ||
| */ | ||
| public sendRequest = <U extends AnySchema>( | ||
| request: RequestT | ServerRequest, | ||
| resultSchema: U, | ||
| options?: RequestOptions | ||
| ): Promise<SchemaOutput<U>> => { | ||
| return this.requestCtx.sendRequest(request, resultSchema, { ...options, relatedRequestId: this.requestId }); | ||
| }; | ||
|
|
||
| /** | ||
| * Sends a request to sample an LLM via the client. | ||
| */ | ||
| public requestSampling(params: CreateMessageRequest['params'], options?: RequestOptions) { | ||
| return this.server.createMessage(params, options); | ||
| } | ||
|
|
||
| /** | ||
| * Sends an elicitation request to the client. | ||
| */ | ||
| public async elicit(params: ElicitRequest['params'], options?: RequestOptions): Promise<ElicitResult> { | ||
| const request: ElicitRequest = { | ||
| method: 'elicitation/create', | ||
| params | ||
| }; | ||
| return await this.server.request(request, ElicitResultSchema, { ...options, relatedRequestId: this.requestId }); | ||
| } | ||
|
|
||
| /** | ||
| * Sends a logging message. | ||
| */ | ||
| public async log(params: LoggingMessageNotification['params'], sessionId?: string) { | ||
| await this.server.sendLoggingMessage(params, sessionId); | ||
| } | ||
|
|
||
| /** | ||
| * Sends a debug log message. | ||
| */ | ||
| public async debug(message: string, extraLogData?: Record<string, unknown>, sessionId?: string) { | ||
| await this.log( | ||
| { | ||
| level: 'debug', | ||
| data: { | ||
| ...extraLogData, | ||
| message | ||
| }, | ||
| logger: 'server' | ||
| }, | ||
| sessionId | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Sends an info log message. | ||
| */ | ||
| public async info(message: string, extraLogData?: Record<string, unknown>, sessionId?: string) { | ||
| await this.log( | ||
| { | ||
| level: 'info', | ||
| data: { | ||
| ...extraLogData, | ||
| message | ||
| }, | ||
| logger: 'server' | ||
| }, | ||
| sessionId | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Sends a warning log message. | ||
| */ | ||
| public async warning(message: string, extraLogData?: Record<string, unknown>, sessionId?: string) { | ||
| await this.log( | ||
| { | ||
| level: 'warning', | ||
| data: { | ||
| ...extraLogData, | ||
| message | ||
| }, | ||
| logger: 'server' | ||
| }, | ||
| sessionId | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Sends an error log message. | ||
| */ | ||
| public async error(message: string, extraLogData?: Record<string, unknown>, sessionId?: string) { | ||
| await this.log( | ||
| { | ||
| level: 'error', | ||
| data: { | ||
| ...extraLogData, | ||
| message | ||
| }, | ||
| logger: 'server' | ||
| }, | ||
| sessionId | ||
| ); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.