Skip to content

Commit e89d9d4

Browse files
committed
moved properties under objects
1 parent 4ff84a1 commit e89d9d4

File tree

2 files changed

+162
-82
lines changed

2 files changed

+162
-82
lines changed

src/server/context.ts

Lines changed: 160 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,118 @@ import { Server } from './index.js';
2020
import { AuthInfo } from './auth/types.js';
2121
import { AnySchema, SchemaOutput } from './zod-compat.js';
2222

23-
export interface ContextInterface<RequestT extends Request = Request, NotificationT extends Notification = Notification>
24-
extends RequestHandlerExtra<ServerRequest | RequestT, NotificationT | ServerNotification> {
25-
elicit(params: ElicitRequest['params'], options?: RequestOptions): Promise<ElicitResult>;
26-
requestSampling: (params: CreateMessageRequest['params'], options?: RequestOptions) => Promise<CreateMessageResult>;
23+
/**
24+
* Interface for sending logging messages to the client via {@link LoggingMessageNotification}.
25+
*/
26+
export interface LoggingMessageSenderInterface {
27+
/**
28+
* Sends a logging message to the client.
29+
*/
2730
log(params: LoggingMessageNotification['params'], sessionId?: string): Promise<void>;
31+
/**
32+
* Sends a debug log message to the client.
33+
*/
2834
debug(message: string, extraLogData?: Record<string, unknown>, sessionId?: string): Promise<void>;
35+
/**
36+
* Sends an info log message to the client.
37+
*/
2938
info(message: string, extraLogData?: Record<string, unknown>, sessionId?: string): Promise<void>;
39+
/**
40+
* Sends a warning log message to the client.
41+
*/
3042
warning(message: string, extraLogData?: Record<string, unknown>, sessionId?: string): Promise<void>;
43+
/**
44+
* Sends an error log message to the client.
45+
*/
3146
error(message: string, extraLogData?: Record<string, unknown>, sessionId?: string): Promise<void>;
3247
}
48+
49+
export class ServerLogger implements LoggingMessageSenderInterface {
50+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
51+
constructor(private readonly server: Server<any, any, any>) {}
52+
53+
/**
54+
* Sends a logging message.
55+
*/
56+
public async log(params: LoggingMessageNotification['params'], sessionId?: string) {
57+
await this.server.sendLoggingMessage(params, sessionId);
58+
}
59+
60+
/**
61+
* Sends a debug log message.
62+
*/
63+
public async debug(message: string, extraLogData?: Record<string, unknown>, sessionId?: string) {
64+
await this.log(
65+
{
66+
level: 'debug',
67+
data: {
68+
...extraLogData,
69+
message
70+
},
71+
logger: 'server'
72+
},
73+
sessionId
74+
);
75+
}
76+
77+
/**
78+
* Sends an info log message.
79+
*/
80+
public async info(message: string, extraLogData?: Record<string, unknown>, sessionId?: string) {
81+
await this.log(
82+
{
83+
level: 'info',
84+
data: {
85+
...extraLogData,
86+
message
87+
},
88+
logger: 'server'
89+
},
90+
sessionId
91+
);
92+
}
93+
94+
/**
95+
* Sends a warning log message.
96+
*/
97+
public async warning(message: string, extraLogData?: Record<string, unknown>, sessionId?: string) {
98+
await this.log(
99+
{
100+
level: 'warning',
101+
data: {
102+
...extraLogData,
103+
message
104+
},
105+
logger: 'server'
106+
},
107+
sessionId
108+
);
109+
}
110+
111+
/**
112+
* Sends an error log message.
113+
*/
114+
public async error(message: string, extraLogData?: Record<string, unknown>, sessionId?: string) {
115+
await this.log(
116+
{
117+
level: 'error',
118+
data: {
119+
...extraLogData,
120+
message
121+
},
122+
logger: 'server'
123+
},
124+
sessionId
125+
);
126+
}
127+
}
128+
129+
export interface ContextInterface<RequestT extends Request = Request, NotificationT extends Notification = Notification>
130+
extends RequestHandlerExtra<ServerRequest | RequestT, NotificationT | ServerNotification> {
131+
elicitInput(params: ElicitRequest['params'], options?: RequestOptions): Promise<ElicitResult>;
132+
requestSampling: (params: CreateMessageRequest['params'], options?: RequestOptions) => Promise<CreateMessageResult>;
133+
logger: LoggingMessageSenderInterface;
134+
}
33135
/**
34136
* A context object that is passed to request handlers.
35137
*
@@ -69,6 +171,31 @@ export class Context<RequestT extends Request = Request, NotificationT extends N
69171
sessionId?: string;
70172
};
71173

174+
public readonly task:
175+
| {
176+
id: string | undefined;
177+
store: RequestTaskStore | undefined;
178+
requestedTtl: number | null | undefined;
179+
}
180+
| undefined;
181+
182+
public readonly stream: {
183+
/**
184+
* Closes the SSE stream for this request, triggering client reconnection.
185+
* Only available when using StreamableHTTPServerTransport with eventStore configured.
186+
* Use this to implement polling behavior during long-running operations.
187+
*/
188+
closeSSEStream: (() => void) | undefined;
189+
/**
190+
* Closes the standalone GET SSE stream, triggering client reconnection.
191+
* Only available when using StreamableHTTPServerTransport with eventStore configured.
192+
* Use this to implement polling behavior for server-initiated notifications.
193+
*/
194+
closeStandaloneSSEStream: (() => void) | undefined;
195+
} | undefined;
196+
197+
public readonly logger: LoggingMessageSenderInterface;
198+
72199
constructor(args: {
73200
server: Server<RequestT, NotificationT, ResultT>;
74201
request: JSONRPCRequest;
@@ -82,6 +209,19 @@ export class Context<RequestT extends Request = Request, NotificationT extends N
82209
_meta: args.requestCtx._meta,
83210
sessionId: args.requestCtx.sessionId
84211
};
212+
213+
this.task = {
214+
id: args.requestCtx.taskId,
215+
store: args.requestCtx.taskStore,
216+
requestedTtl: args.requestCtx.taskRequestedTtl
217+
};
218+
219+
this.logger = new ServerLogger(args.server);
220+
221+
this.stream = {
222+
closeSSEStream: args.requestCtx.closeSSEStream,
223+
closeStandaloneSSEStream: args.requestCtx.closeStandaloneSSEStream
224+
};
85225
}
86226

87227
/**
@@ -120,22 +260,37 @@ export class Context<RequestT extends Request = Request, NotificationT extends N
120260
return this.mcpContext.sessionId;
121261
}
122262

263+
/**
264+
* @deprecated Use {@link task.id} instead.
265+
*/
123266
public get taskId(): string | undefined {
124267
return this.requestCtx.taskId;
125268
}
126269

270+
/**
271+
* @deprecated Use {@link task.store} instead.
272+
*/
127273
public get taskStore(): RequestTaskStore | undefined {
128274
return this.requestCtx.taskStore;
129275
}
130276

277+
/**
278+
* @deprecated Use {@link task.requestedTtl} instead.
279+
*/
131280
public get taskRequestedTtl(): number | undefined {
132281
return this.requestCtx.taskRequestedTtl ?? undefined;
133282
}
134283

284+
/**
285+
* @deprecated Use {@link stream.closeSSEStream} instead.
286+
*/
135287
public get closeSSEStream(): (() => void) | undefined {
136288
return this.requestCtx.closeSSEStream;
137289
}
138290

291+
/**
292+
* @deprecated Use {@link stream.closeStandaloneSSEStream} instead.
293+
*/
139294
public get closeStandaloneSSEStream(): (() => void) | undefined {
140295
return this.requestCtx.closeStandaloneSSEStream;
141296
}
@@ -172,86 +327,11 @@ export class Context<RequestT extends Request = Request, NotificationT extends N
172327
/**
173328
* Sends an elicitation request to the client.
174329
*/
175-
public async elicit(params: ElicitRequest['params'], options?: RequestOptions): Promise<ElicitResult> {
330+
public async elicitInput(params: ElicitRequest['params'], options?: RequestOptions): Promise<ElicitResult> {
176331
const request: ElicitRequest = {
177332
method: 'elicitation/create',
178333
params
179334
};
180335
return await this.server.request(request, ElicitResultSchema, { ...options, relatedRequestId: this.requestId });
181336
}
182-
183-
/**
184-
* Sends a logging message.
185-
*/
186-
public async log(params: LoggingMessageNotification['params'], sessionId?: string) {
187-
await this.server.sendLoggingMessage(params, sessionId);
188-
}
189-
190-
/**
191-
* Sends a debug log message.
192-
*/
193-
public async debug(message: string, extraLogData?: Record<string, unknown>, sessionId?: string) {
194-
await this.log(
195-
{
196-
level: 'debug',
197-
data: {
198-
...extraLogData,
199-
message
200-
},
201-
logger: 'server'
202-
},
203-
sessionId
204-
);
205-
}
206-
207-
/**
208-
* Sends an info log message.
209-
*/
210-
public async info(message: string, extraLogData?: Record<string, unknown>, sessionId?: string) {
211-
await this.log(
212-
{
213-
level: 'info',
214-
data: {
215-
...extraLogData,
216-
message
217-
},
218-
logger: 'server'
219-
},
220-
sessionId
221-
);
222-
}
223-
224-
/**
225-
* Sends a warning log message.
226-
*/
227-
public async warning(message: string, extraLogData?: Record<string, unknown>, sessionId?: string) {
228-
await this.log(
229-
{
230-
level: 'warning',
231-
data: {
232-
...extraLogData,
233-
message
234-
},
235-
logger: 'server'
236-
},
237-
sessionId
238-
);
239-
}
240-
241-
/**
242-
* Sends an error log message.
243-
*/
244-
public async error(message: string, extraLogData?: Record<string, unknown>, sessionId?: string) {
245-
await this.log(
246-
{
247-
level: 'error',
248-
data: {
249-
...extraLogData,
250-
message
251-
},
252-
logger: 'server'
253-
},
254-
sessionId
255-
);
256-
}
257337
}

test/server/context.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ describe('Context', () => {
138138
});
139139

140140
mcpServer.registerTool('ctx-log-test', { inputSchema: z.object({ name: z.string() }) }, async (_args: { name: string }, extra) => {
141-
await extra[level]('Test message', { test: 'test' }, 'sample-session-id');
142-
await extra.log(
141+
await extra.logger[level]('Test message', { test: 'test' }, 'sample-session-id');
142+
await extra.logger.log(
143143
{
144144
level,
145145
data: 'Test message',

0 commit comments

Comments
 (0)