import Anthropic from "@anthropic-ai/sdk";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
const ROLLOUT_MCP_URL = new URL("https://universal.rollout.com/mcp");
const ROLLOUT_AUTH_TOKEN = "<YOUR ROLLOUT AUTH TOKEN HERE>";
const ANTHROPIC_API_KEY = "<YOUR ANTHROPIC API KEY HERE>";
async function sendQueryWithTools(client) {
const anthropic = new Anthropic({
apiKey: ANTHROPIC_API_KEY,
});
const messages = [
{
role: "user",
content: "Which leads should I reach out to today?",
},
];
// Get the list of tools available from the MCP server
const toolsResponse = await client.listTools();
const availableTools = toolsResponse.tools.map((t) => ({
name: t.name,
description: t.description,
input_schema: t.inputSchema,
}));
// Send user message to the model along with provided tools
let modelResponse = await anthropic.messages.create({
model: "claude-3-5-sonnet-20241022",
max_tokens: 1000,
messages,
tools: availableTools,
});
for (const content of modelResponse.content) {
if (content.type === "tool_use") {
// Make call to MCP server on the model's behalf
const result = await client.callTool({
name: content.name,
arguments: content.input,
});
// Provide prior conversation context and tool use result back to the model
messages.push({
role: "assistant",
content: [content],
});
messages.push({
role: "user",
content: [
{
type: "tool_result",
tool_use_id: content.id,
content: result.content,
},
],
});
// Get next response from the model after tool use
modelResponse = await anthropic.messages.create({
model: "claude-3-5-sonnet-20241022",
max_tokens: 1000,
messages,
tools: availableTools,
});
}
}
// Print the final response text
console.log(modelResponse.content[0].text);
}
async function withRolloutMcpClient(token, fn) {
const client = await createRolloutMcpClient(token);
try {
await fn(client);
} finally {
client.close();
}
}
async function createRolloutMcpClient(rolloutAuthJwt) {
const transport = new StreamableHTTPClientTransport(ROLLOUT_MCP_URL, {
requestInit: {
headers: { Authorization: `Bearer ${rolloutAuthJwt}` },
},
});
const client = new Client({
name: "rollout-client",
version: "0.0.1",
});
await client.connect(transport);
return client;
}
withRolloutMcpClient(ROLLOUT_AUTH_TOKEN, sendQueryWithTools);