|
| 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