From 443c0829ed931147d4a953919d2ae52c6b7b1ac2 Mon Sep 17 00:00:00 2001 From: Kalvin Chau Date: Wed, 22 Jan 2025 09:17:59 -0800 Subject: [PATCH 1/2] feat: add mcp client instruction support --- src/client/index.test.ts | 7 +++++++ src/client/index.ts | 10 ++++++++++ src/types.ts | 6 ++++++ 3 files changed, 23 insertions(+) diff --git a/src/client/index.test.ts b/src/client/index.test.ts index 55a113ea..1209b60c 100644 --- a/src/client/index.test.ts +++ b/src/client/index.test.ts @@ -36,6 +36,7 @@ test("should initialize with matching protocol version", async () => { name: "test", version: "1.0", }, + instructions: "test instructions", }, }); } @@ -66,6 +67,9 @@ test("should initialize with matching protocol version", async () => { }), }), ); + + // Should have the instructions returned + expect(client.getInstructions()).toEqual("test instructions"); }); test("should initialize with supported older protocol version", async () => { @@ -111,6 +115,9 @@ test("should initialize with supported older protocol version", async () => { name: "test", version: "1.0", }); + + // Expect no instructions + expect(client.getInstructions()).toBeUndefined(); }); test("should reject unsupported protocol version", async () => { diff --git a/src/client/index.ts b/src/client/index.ts index da0d04c5..d4b17af1 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -85,6 +85,7 @@ export class Client< private _serverCapabilities?: ServerCapabilities; private _serverVersion?: Implementation; private _capabilities: ClientCapabilities; + private _instructions?: string; /** * Initializes this client with the given name and version information. @@ -152,6 +153,8 @@ export class Client< this._serverCapabilities = result.capabilities; this._serverVersion = result.serverInfo; + this._instructions = result.instructions; + await this.notification({ method: "notifications/initialized", }); @@ -176,6 +179,13 @@ export class Client< return this._serverVersion; } + /** + * After initialization has completed, this may be populated with information about the server's instructions. + */ + getInstructions(): String | undefined { + return this._instructions; + } + protected assertCapabilityForMethod(method: RequestT["method"]): void { switch (method as ClientRequest["method"]) { case "logging/setLevel": diff --git a/src/types.ts b/src/types.ts index 15acd149..afefb5b8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -304,6 +304,12 @@ export const InitializeResultSchema = ResultSchema.extend({ protocolVersion: z.string(), capabilities: ServerCapabilitiesSchema, serverInfo: ImplementationSchema, + /** + * Instructions describing how to use the server and its features. + * + * This can be used by clients to improve the LLM's understanding of available tools, resources, etc. It can be thought of like a "hint" to the model. For example, this information MAY be added to the system prompt. + */ + instructions: z.optional(z.string()), }); /** From 50870871908727e7f860874a9c7cf8f5eb9d1b70 Mon Sep 17 00:00:00 2001 From: Kalvin Chau Date: Wed, 22 Jan 2025 09:25:28 -0800 Subject: [PATCH 2/2] style: fix linting error on String vs string --- src/client/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/index.ts b/src/client/index.ts index d4b17af1..bcad952c 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -182,7 +182,7 @@ export class Client< /** * After initialization has completed, this may be populated with information about the server's instructions. */ - getInstructions(): String | undefined { + getInstructions(): string | undefined { return this._instructions; }