Skip to content

Add header to MCP server to expand authentication capabilities #229

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,10 @@ export class Client<
| typeof CallToolResultSchema
| typeof CompatibilityCallToolResultSchema = CallToolResultSchema,
options?: RequestOptions,
headers?: Record<string, string | boolean | number>
) {
return this.request(
{ method: "tools/call", params },
{ method: "tools/call", params, headers },
resultSchema,
options,
);
Expand Down
7 changes: 4 additions & 3 deletions src/server/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class McpServer {
const args = parseResult.data;
const cb = tool.callback as ToolCallback<ZodRawShape>;
try {
return await Promise.resolve(cb(args, extra));
return await Promise.resolve(cb(args, extra, request.headers));
} catch (error) {
return {
content: [
Expand All @@ -158,7 +158,7 @@ export class McpServer {
} else {
const cb = tool.callback as ToolCallback<undefined>;
try {
return await Promise.resolve(cb(extra));
return await Promise.resolve(cb(extra, request.headers));
} catch (error) {
return {
content: [
Expand Down Expand Up @@ -695,8 +695,9 @@ export type ToolCallback<Args extends undefined | ZodRawShape = undefined> =
? (
args: z.objectOutputType<Args, ZodTypeAny>,
extra: RequestHandlerExtra,
headers: Record<string, string | number | boolean> | undefined
) => CallToolResult | Promise<CallToolResult>
: (extra: RequestHandlerExtra) => CallToolResult | Promise<CallToolResult>;
: (extra: RequestHandlerExtra, headers: Record<string, string | number | boolean> | undefined) => CallToolResult | Promise<CallToolResult>;

type RegisteredTool = {
description?: string;
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const BaseRequestParamsSchema = z

export const RequestSchema = z.object({
method: z.string(),
headers: z.optional(z.record(z.union([z.string(), z.number(), z.boolean()]))),
params: z.optional(BaseRequestParamsSchema),
});

Expand Down