Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/cold-bears-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"agents": patch
---

Add unstable_getMcpPrompt to provide LLM consumable MCP integration context
28 changes: 18 additions & 10 deletions packages/agents/src/mcp/client-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { SSEEdgeClientTransport } from "./sse-edge";

import {
ToolListChangedNotificationSchema,
type ClientCapabilities,
type Resource,
type Tool,
type Prompt,
Expand All @@ -14,7 +13,7 @@ import {
type ServerCapabilities,
type ResourceTemplate,
type ListResourceTemplatesResult,
type Notification,
type Implementation,
} from "@modelcontextprotocol/sdk/types.js";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import type { SSEClientTransportOptions } from "@modelcontextprotocol/sdk/client/sse.js";
Expand All @@ -34,6 +33,7 @@ export class MCPClientConnection {
resources: Resource[] = [];
resourceTemplates: ResourceTemplate[] = [];
serverCapabilities: ServerCapabilities | undefined;
serverInfo: Implementation | undefined;

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

const [instructions, tools, resources, prompts, resourceTemplates] =
await Promise.all([
this.client.getInstructions(),
this.registerTools(),
this.registerResources(),
this.registerPrompts(),
this.registerResourceTemplates(),
]);
const [
instructions,
serverInfo,
tools,
resources,
prompts,
resourceTemplates,
] = await Promise.all([
this.client.getInstructions(),
this.client.getServerVersion(),
this.registerTools(),
this.registerResources(),
this.registerPrompts(),
this.registerResourceTemplates(),
]);

this.instructions = instructions;
this.tools = tools;
this.resources = resources;
this.prompts = prompts;
this.resourceTemplates = resourceTemplates;
this.serverInfo = serverInfo;

this.connectionState = "ready";
}
Expand Down
34 changes: 34 additions & 0 deletions packages/agents/src/mcp/client-prompt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { MCPClientConnection } from "./client-connection";
import type { Resource } from "@modelcontextprotocol/sdk/types.js";

export function unstable_getMcpPrompt(
conns: Record<string, MCPClientConnection>,
includeResources: boolean
) {
return `<integrations_list>
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

retrieive -> retrieve
which you can use to execute -> which you can execute

${includeResources && "Each integration, provides a list of resources, which are included in the list of integrations below."}
Here is a list of all of the integrations you have access to, with instructions if necessary:
${Object.entries(conns).map(([_id, conn]) => serverContext(conn, includeResources))}
<integrations_list>`;
}

function serverContext(conn: MCPClientConnection, includeResources: boolean) {
return `<integration>
${conn.serverInfo && `<integration_name>${conn.serverInfo.name}</integration_name>`}
${conn.instructions && `<integration_instructions>${conn.instructions}</integration_instructions>`}
${includeResources && `<resources_list>${conn.resources.map((resource) => resourceContext(resource))}</resources_list>`}
<integration>`;
}

function resourceContext(resource: Resource) {
return `<resource>
<name>${resource.name}</name>
<uri>${resource.uri}</uri>
<description>${resource.description}</description>
<mimeType>${resource.mimeType}</mimeType>
</resource>`;
}
10 changes: 10 additions & 0 deletions packages/agents/src/mcp/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { RequestOptions } from "@modelcontextprotocol/sdk/shared/protocol.j
import type { AgentsOAuthProvider } from "./do-oauth-client-provider";
import { jsonSchema, type ToolSet } from "ai";
import { nanoid } from "nanoid";
import { unstable_getMcpPrompt } from "./client-prompt";

/**
* Utility class that aggregates multiple MCP clients into one
Expand Down Expand Up @@ -301,6 +302,15 @@ export class MCPClientManager {
options
);
}

/**
* Utility function to fetch MCP information which you can add to an LLM's system prompt.
*
* @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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function no longer has a parameter

*/
unstable_getMcpPrompt(includeResources = true): string {
return unstable_getMcpPrompt(this.mcpConnections, includeResources);
}
}

type NamespacedData = {
Expand Down