Skip to content

Commit 7a6f108

Browse files
update documentation for mcp
1 parent 5b6665f commit 7a6f108

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

astro.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export default defineConfig({
2828
label: "Class Reference",
2929
items: [
3030
{ label: "Loom", slug: "reference/loom" },
31+
{ label: "MCP", slug: "reference/mcp" },
3132
{ label: "Agent", slug: "reference/agent" },
3233
{ label: "Runner", slug: "reference/runner" },
3334
{ label: "Trace", slug: "reference/trace" },

src/content/docs/index.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ import { Card, CardGrid } from "@astrojs/starlight/components";
5252
Track every step of your agent's execution with detailed, hierarchical
5353
traces.
5454
</Card>
55+
<Card title="MCP Integration" icon="cloud-download">
56+
Leverage third party integrations to take your agents to the next level
57+
</Card>
5558
<Card title="Tool Integration" icon="puzzle">
5659
Extend your agents with custom tools for database access, API calls, and
5760
more.

src/content/docs/reference/agent.mdx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ interface AgentConfig {
3737
sub_agents?: Agent[]; // Optional: Sub-agents that can be called
3838
tools?: ToolCall[]; // Optional: Tools the agent can use
3939
model?: string; // Optional: Default is 'gpt-4o'
40+
mcp_servers?: (MCPServerSSE | MCPServerStdio)[];
4041
web_search?: WebSearchConfig; // Optional: Web search configuration
4142
timeout_ms?: number; // Optional: Default is 60000ms (1 minute)
4243
}
@@ -88,6 +89,32 @@ const result = await agent.run(
8889
);
8990
```
9091

92+
## Working with MCP Servers
93+
94+
Loom supports integrating with external tools using the [Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction), a standard that allows third-party applications and services to provide context and capabilities to LLMs.
95+
96+
You can use two types of MCP connections:
97+
98+
- [`MCPServerSSE`](../reference/mcp) – Connects to an MCP tool server over **Server-Sent Events**
99+
- [`MCPServerStdio`](../reference/mcp) – Connects to a local MCP tool server via **stdio**
100+
101+
### Example: Connecting to MCP Servers
102+
103+
```ts
104+
import { Agent, MCPServerSSE, MCPServerStdio } from "loom-agents";
105+
106+
const researchAgent = new Agent({
107+
name: "MCP Tool Runner",
108+
purpose: "Run a connected MCP tool!",
109+
mcp_servers: [
110+
new MCPServerSSE(new URL("http://localhost:3001/sse")),
111+
new MCPServerStdio("bun", ["stdio.ts"]),
112+
],
113+
});
114+
```
115+
116+
With this configuration, your agent can call tools exposed by the connected MCP servers seamlessly.
117+
91118
## Defining Tools
92119

93120
Tools allow agents to interact with external systems or perform specialized tasks:

src/content/docs/reference/mcp.mdx

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
---
2+
title: MCPServer
3+
description: Base class for connecting to Model Context Protocol-compatible tool servers over SSE or stdio
4+
---
5+
6+
The `MCPServer` system provides a standardized way to connect to tool servers that implement the [Model Context Protocol (MCP)](https://modelcontext.org). It supports both **SSE (Server-Sent Events)** and **stdio (standard input/output)** transports.
7+
8+
## Creating a Server Connection
9+
10+
There are two built-in classes for connection:
11+
12+
- `MCPServerSSE`: Connects to a remote MCP server using Server-Sent Events.
13+
- `MCPServerStdio`: Launches and communicates with a local MCP server using standard input/output.
14+
15+
```ts
16+
import { MCPServerSSE, MCPServerStdio } from "loom-agents/mcp";
17+
18+
// Using SSE
19+
const mcpSSE = new MCPServerSSE(new URL("http://localhost:3000/mcp"));
20+
21+
// Using stdio
22+
const mcpStdio = new MCPServerStdio("node", ["./tool-server.js"]);
23+
```
24+
25+
## Listing Available Tools
26+
27+
Each server may expose a set of tools that can be queried:
28+
29+
```ts
30+
const tools = await mcpSSE.getTools();
31+
console.log(tools);
32+
// {
33+
// tools: [
34+
// {
35+
// name: "calculate-bmi",
36+
// inputSchema: {
37+
// type: "object",
38+
// properties: [Object],
39+
// required: [Array],
40+
// additionalProperties: false,
41+
// $schema: "http://json-schema.org/draft-07/schema#",
42+
// },
43+
// },
44+
// {
45+
// name: "fetch-weather",
46+
// inputSchema: {
47+
// type: "object",
48+
// properties: [Object],
49+
// required: [Array],
50+
// additionalProperties: false,
51+
// $schema: "http://json-schema.org/draft-07/schema#",
52+
// },
53+
// },
54+
// ];
55+
// }
56+
```
57+
58+
This will automatically connect the client if it hasn’t already.
59+
60+
## Calling a Tool
61+
62+
Once connected, tools can be called directly by name:
63+
64+
```ts
65+
const result = await mcpSSE.callTool({
66+
name: "calculate-bmi",
67+
arguments: {
68+
"weightKg":70,
69+
"heightM":1.75
70+
}
71+
});
72+
73+
console.log(result);
74+
// {
75+
// content: [
76+
// {
77+
// type: "text",
78+
// text: "22.857142857142858",
79+
// },
80+
// ];
81+
// }
82+
83+
// Note, Loom parses MCP results for errors and sends the content / output off `.content`
84+
// [
85+
// {
86+
// type: "text",
87+
// text: "22.857142857142858",
88+
// },
89+
//]
90+
```
91+
92+
## Extending MCPServer
93+
94+
If you want to implement a custom transport or extend tool behaviors, you can subclass `MCPServerBase`.
95+
96+
```ts
97+
class CustomMCPServer extends MCPServerBase {
98+
constructor(myTransport: CustomTransport) {
99+
super(myTransport);
100+
}
101+
102+
// Custom behavior
103+
}
104+
```
105+
106+
## Internals
107+
108+
The `MCPServerBase` manages client connection state and handles listing and calling tools. It lazily initializes the client only when needed:
109+
110+
```ts
111+
await this.client.connect(this.transport);
112+
```
113+
114+
It caches tool metadata after the first call to `getTools()` to avoid redundant lookups.
115+
116+
## Related
117+
118+
- [Agent](/docs/reference/agent): Interacts with tools through an MCPServer
119+
- [Tool Definition](/docs/reference/tool): Learn how tools are defined and exposed
120+
- [Trace](/docs/reference/trace): Structured tracking of tool calls and agent interactions
121+
- [Loom Configuration](/docs/reference/loom): Customize global AI behavior and context handling

0 commit comments

Comments
 (0)