Skip to content

Commit 4320c94

Browse files
committed
Add unstable_getMcpPrompt to provide LLM consumable MCP integration context
1 parent 54e1986 commit 4320c94

File tree

4 files changed

+67
-10
lines changed

4 files changed

+67
-10
lines changed

.changeset/cold-bears-behave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"agents": patch
3+
---
4+
5+
Add unstable_getMcpPrompt to provide LLM consumable MCP integration context

packages/agents/src/mcp/client-connection.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { SSEEdgeClientTransport } from "./sse-edge";
22

33
import {
44
ToolListChangedNotificationSchema,
5-
type ClientCapabilities,
65
type Resource,
76
type Tool,
87
type Prompt,
@@ -14,7 +13,7 @@ import {
1413
type ServerCapabilities,
1514
type ResourceTemplate,
1615
type ListResourceTemplatesResult,
17-
type Notification,
16+
type Implementation,
1817
} from "@modelcontextprotocol/sdk/types.js";
1918
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
2019
import type { SSEClientTransportOptions } from "@modelcontextprotocol/sdk/client/sse.js";
@@ -34,6 +33,7 @@ export class MCPClientConnection {
3433
resources: Resource[] = [];
3534
resourceTemplates: ResourceTemplate[] = [];
3635
serverCapabilities: ServerCapabilities | undefined;
36+
serverInfo: Implementation | undefined;
3737

3838
constructor(
3939
public url: URL,
@@ -84,20 +84,28 @@ export class MCPClientConnection {
8484
throw new Error("The MCP Server failed to return server capabilities");
8585
}
8686

87-
const [instructions, tools, resources, prompts, resourceTemplates] =
88-
await Promise.all([
89-
this.client.getInstructions(),
90-
this.registerTools(),
91-
this.registerResources(),
92-
this.registerPrompts(),
93-
this.registerResourceTemplates(),
94-
]);
87+
const [
88+
instructions,
89+
serverInfo,
90+
tools,
91+
resources,
92+
prompts,
93+
resourceTemplates,
94+
] = await Promise.all([
95+
this.client.getInstructions(),
96+
this.client.getServerVersion(),
97+
this.registerTools(),
98+
this.registerResources(),
99+
this.registerPrompts(),
100+
this.registerResourceTemplates(),
101+
]);
95102

96103
this.instructions = instructions;
97104
this.tools = tools;
98105
this.resources = resources;
99106
this.prompts = prompts;
100107
this.resourceTemplates = resourceTemplates;
108+
this.serverInfo = serverInfo;
101109

102110
this.connectionState = "ready";
103111
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { MCPClientConnection } from "./client-connection";
2+
import type { Resource } from "@modelcontextprotocol/sdk/types.js";
3+
4+
export function unstable_getMcpPrompt(
5+
conns: Record<string, MCPClientConnection>,
6+
includeResources: boolean
7+
) {
8+
return `<integrations_list>
9+
You have access to multiple integrations via Model Context Protocol (MCP). These integrations provide you with tools which you can use to execute to complete tasks or retrieive information.
10+
11+
${includeResources && "Each integration, provides a list of resources, which are included in the list of integrations below."}
12+
13+
Here is a list of all of the integrations you have access to, with instructions if necessary:
14+
15+
${Object.entries(conns).map(([_id, conn]) => serverContext(conn, includeResources))}
16+
<integrations_list>`;
17+
}
18+
19+
function serverContext(conn: MCPClientConnection, includeResources: boolean) {
20+
return `<integration>
21+
${conn.serverInfo && `<integration_name>${conn.serverInfo.name}</integration_name>`}
22+
${conn.instructions && `<integration_instructions>${conn.instructions}</integration_instructions>`}
23+
${includeResources && `<resources_list>${conn.resources.map((resource) => resourceContext(resource))}</resources_list>`}
24+
<integration>`;
25+
}
26+
27+
function resourceContext(resource: Resource) {
28+
return `<resource>
29+
<name>${resource.name}</name>
30+
<uri>${resource.uri}</uri>
31+
<description>${resource.description}</description>
32+
<mimeType>${resource.mimeType}</mimeType>
33+
</resource>`;
34+
}

packages/agents/src/mcp/client.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type { RequestOptions } from "@modelcontextprotocol/sdk/shared/protocol.j
1717
import type { AgentsOAuthProvider } from "./do-oauth-client-provider";
1818
import { jsonSchema, type ToolSet } from "ai";
1919
import { nanoid } from "nanoid";
20+
import { unstable_getMcpPrompt } from "./client-prompt";
2021

2122
/**
2223
* Utility class that aggregates multiple MCP clients into one
@@ -301,6 +302,15 @@ export class MCPClientManager {
301302
options
302303
);
303304
}
305+
306+
/**
307+
* Utility function to fetch MCP information which you can add to an LLM's system prompt.
308+
*
309+
* @param includeResources Whether to include resources in the prompt. This may be useful if you include a tool to fetch resources OR if the servers you connect to interact with resources.
310+
*/
311+
unstable_getMcpPrompt(includeResources = true): string {
312+
return unstable_getMcpPrompt(this.mcpConnections, includeResources);
313+
}
304314
}
305315

306316
type NamespacedData = {

0 commit comments

Comments
 (0)