@mastra/mcp 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/README.md +9 -774
- package/dist/__fixtures__/tools.d.ts +1 -1
- package/dist/__fixtures__/tools.d.ts.map +1 -1
- package/dist/docs/SKILL.md +16 -34
- package/dist/docs/{SOURCE_MAP.json → assets/SOURCE_MAP.json} +1 -1
- package/dist/docs/{mcp/01-overview.md → references/docs-mcp-overview.md} +140 -142
- package/dist/docs/references/docs-mcp-publishing-mcp-server.md +95 -0
- package/dist/docs/references/reference-tools-mcp-client.md +962 -0
- package/dist/docs/{tools/01-reference.md → references/reference-tools-mcp-server.md} +163 -1194
- package/dist/index.cjs +6 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +6 -6
- package/dist/index.js.map +1 -1
- package/package.json +8 -9
- package/dist/docs/README.md +0 -33
- package/dist/docs/mcp/02-publishing-mcp-server.md +0 -111
- package/dist/docs/tools-mcp/01-mcp-overview.md +0 -384
|
@@ -8,5 +8,5 @@ export declare const weatherTool: import("@mastra/core/tools").Tool<{
|
|
|
8
8
|
windGust: number;
|
|
9
9
|
conditions: string;
|
|
10
10
|
location: string;
|
|
11
|
-
}, unknown, unknown, import("@mastra/core/tools").ToolExecutionContext<unknown, unknown>, "get-weather">;
|
|
11
|
+
}, unknown, unknown, import("@mastra/core/tools").ToolExecutionContext<unknown, unknown, unknown>, "get-weather", unknown>;
|
|
12
12
|
//# sourceMappingURL=tools.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../src/__fixtures__/tools.ts"],"names":[],"mappings":"AAsBA,eAAO,MAAM,WAAW;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../src/__fixtures__/tools.ts"],"names":[],"mappings":"AAsBA,eAAO,MAAM,WAAW;;;;;;;;;;0HAUtB,CAAC"}
|
package/dist/docs/SKILL.md
CHANGED
|
@@ -1,46 +1,28 @@
|
|
|
1
1
|
---
|
|
2
|
-
name: mastra-mcp
|
|
3
|
-
description: Documentation for @mastra/mcp.
|
|
2
|
+
name: mastra-mcp
|
|
3
|
+
description: Documentation for @mastra/mcp. Use when working with @mastra/mcp APIs, configuration, or implementation.
|
|
4
|
+
metadata:
|
|
5
|
+
package: "@mastra/mcp"
|
|
6
|
+
version: "1.0.1"
|
|
4
7
|
---
|
|
5
8
|
|
|
6
|
-
|
|
9
|
+
## When to use
|
|
7
10
|
|
|
8
|
-
|
|
9
|
-
> **Package**: @mastra/mcp
|
|
11
|
+
Use this skill whenever you are working with @mastra/mcp to obtain the domain-specific knowledge.
|
|
10
12
|
|
|
11
|
-
##
|
|
13
|
+
## How to use
|
|
12
14
|
|
|
13
|
-
|
|
15
|
+
Read the individual reference documents for detailed explanations and code examples.
|
|
14
16
|
|
|
15
|
-
|
|
16
|
-
cat docs/SOURCE_MAP.json
|
|
17
|
-
```
|
|
17
|
+
### Docs
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
-
|
|
21
|
-
- **implementation**: `.js` chunk file with readable source
|
|
22
|
-
- **docs**: Conceptual documentation in `docs/`
|
|
19
|
+
- [MCP Overview](references/docs-mcp-overview.md) - Learn about the Model Context Protocol (MCP), how to use third-party tools via MCPClient, connect to registries, and share your own tools using MCPServer.
|
|
20
|
+
- [Publishing an MCP Server](references/docs-mcp-publishing-mcp-server.md) - Guide to setting up and building a Mastra MCP server using the stdio transport, and publishing it to NPM.
|
|
23
21
|
|
|
24
|
-
|
|
22
|
+
### Reference
|
|
25
23
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
- buildDiscoveryUrls: dist/index.d.ts
|
|
29
|
-
- discoverAuthorizationServerMetadata: dist/index.d.ts
|
|
30
|
-
- discoverOAuthMetadata: dist/index.d.ts
|
|
31
|
-
- discoverOAuthProtectedResourceMetadata: dist/index.d.ts
|
|
32
|
-
- exchangeAuthorization: dist/index.d.ts
|
|
33
|
-
- extractResourceMetadataUrl: dist/index.d.ts
|
|
34
|
-
- parseErrorResponse: dist/index.d.ts
|
|
35
|
-
- refreshAuthorization: dist/index.d.ts
|
|
36
|
-
- registerClient: dist/index.d.ts
|
|
37
|
-
- selectResourceURL: dist/index.d.ts
|
|
38
|
-
- startAuthorization: dist/index.d.ts
|
|
24
|
+
- [Reference: MCPClient](references/reference-tools-mcp-client.md) - API Reference for MCPClient - A class for managing multiple Model Context Protocol servers and their tools.
|
|
25
|
+
- [Reference: MCPServer](references/reference-tools-mcp-server.md) - API Reference for MCPServer - A class for exposing Mastra tools and capabilities as a Model Context Protocol server.
|
|
39
26
|
|
|
40
|
-
See SOURCE_MAP.json for the complete list.
|
|
41
27
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
- [Mcp](mcp/) - 2 file(s)
|
|
45
|
-
- [Tools](tools/) - 3 file(s)
|
|
46
|
-
- [Tools mcp](tools-mcp/) - 1 file(s)
|
|
28
|
+
Read [assets/SOURCE_MAP.json](assets/SOURCE_MAP.json) for source code references.
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
> Learn about the Model Context Protocol (MCP), how to use third-party tools via MCPClient, connect to registries, and share your own tools using MCPServer.
|
|
2
|
-
|
|
3
1
|
# MCP Overview
|
|
4
2
|
|
|
5
3
|
Mastra supports the [Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction), an open standard for connecting AI agents to external tools and resources. It serves as a universal plugin system, enabling agents to call tools regardless of language or hosting environment.
|
|
@@ -8,22 +6,42 @@ Mastra can also be used to author MCP servers, exposing agents, tools, and other
|
|
|
8
6
|
|
|
9
7
|
Mastra currently supports two MCP classes:
|
|
10
8
|
|
|
11
|
-
1.
|
|
12
|
-
2.
|
|
9
|
+
1. `MCPClient`: Connects to one or many MCP servers to access their tools, resources, prompts, and handle elicitation requests.
|
|
10
|
+
2. `MCPServer`: Exposes Mastra tools, agents, workflows, prompts, and resources to MCP-compatible clients.
|
|
13
11
|
|
|
14
12
|
## Getting started
|
|
15
13
|
|
|
16
14
|
To use MCP, install the required dependency:
|
|
17
15
|
|
|
16
|
+
**npm**:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @mastra/mcp@latest
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
**pnpm**:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pnpm add @mastra/mcp@latest
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
**Yarn**:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
yarn add @mastra/mcp@latest
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
**Bun**:
|
|
35
|
+
|
|
18
36
|
```bash
|
|
19
|
-
|
|
37
|
+
bun add @mastra/mcp@latest
|
|
20
38
|
```
|
|
21
39
|
|
|
22
40
|
## Configuring `MCPClient`
|
|
23
41
|
|
|
24
42
|
The `MCPClient` connects Mastra primitives to external MCP servers, which can be local packages (invoked using `npx`) or remote HTTP(S) endpoints. Each server must be configured with either a `command` or a `url`, depending on how it's hosted.
|
|
25
43
|
|
|
26
|
-
```typescript
|
|
44
|
+
```typescript
|
|
27
45
|
import { MCPClient } from "@mastra/mcp";
|
|
28
46
|
|
|
29
47
|
export const testMcpClient = new MCPClient({
|
|
@@ -42,21 +60,16 @@ export const testMcpClient = new MCPClient({
|
|
|
42
60
|
});
|
|
43
61
|
```
|
|
44
62
|
|
|
45
|
-
> **
|
|
63
|
+
> **Info:** Visit [MCPClient](https://mastra.ai/reference/tools/mcp-client) for a full list of configuration options.
|
|
46
64
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
> **Note:** Authentication
|
|
50
|
-
|
|
51
|
-
For connecting to OAuth-protected MCP servers, see the [OAuth Authentication](https://mastra.ai/reference/v1/tools/mcp-client#oauth-authentication) section.
|
|
65
|
+
> **Authentication:** For connecting to OAuth-protected MCP servers, see the [OAuth Authentication](https://mastra.ai/reference/tools/mcp-client) section.
|
|
52
66
|
|
|
53
67
|
## Using `MCPClient` with an agent
|
|
54
68
|
|
|
55
69
|
To use tools from an MCP server in an agent, import your `MCPClient` and call `.listTools()` in the `tools` parameter. This loads from the defined MCP servers, making them available to the agent.
|
|
56
70
|
|
|
57
|
-
```typescript
|
|
71
|
+
```typescript
|
|
58
72
|
import { Agent } from "@mastra/core/agent";
|
|
59
|
-
|
|
60
73
|
import { testMcpClient } from "../mcp/test-mcp-client";
|
|
61
74
|
|
|
62
75
|
export const testAgent = new Agent({
|
|
@@ -74,15 +87,13 @@ export const testAgent = new Agent({
|
|
|
74
87
|
});
|
|
75
88
|
```
|
|
76
89
|
|
|
77
|
-
> **
|
|
78
|
-
|
|
79
|
-
Visit [Agent Class](https://mastra.ai/reference/v1/agents/agent) for a full list of configuration options.
|
|
90
|
+
> **Info:** Visit [Agent Class](https://mastra.ai/reference/agents/agent) for a full list of configuration options.
|
|
80
91
|
|
|
81
92
|
## Configuring `MCPServer`
|
|
82
93
|
|
|
83
94
|
To expose agents, tools, and workflows from your Mastra application to external systems over HTTP(S) use the `MCPServer` class. This makes them accessible to any system or agent that supports the protocol.
|
|
84
95
|
|
|
85
|
-
```typescript
|
|
96
|
+
```typescript
|
|
86
97
|
import { MCPServer } from "@mastra/mcp";
|
|
87
98
|
|
|
88
99
|
import { testAgent } from "../agents/test-agent";
|
|
@@ -99,19 +110,15 @@ export const testMcpServer = new MCPServer({
|
|
|
99
110
|
});
|
|
100
111
|
```
|
|
101
112
|
|
|
102
|
-
> **
|
|
113
|
+
> **Info:** Visit [MCPServer](https://mastra.ai/reference/tools/mcp-server) for a full list of configuration options.
|
|
103
114
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
> **Note:** Authentication
|
|
107
|
-
|
|
108
|
-
To protect your MCP server with OAuth, see the [OAuth Protection](https://mastra.ai/reference/v1/tools/mcp-server#oauth-protection) section.
|
|
115
|
+
> **Authentication:** To protect your MCP server with OAuth, see the [OAuth Protection](https://mastra.ai/reference/tools/mcp-server) section.
|
|
109
116
|
|
|
110
117
|
## Registering an `MCPServer`
|
|
111
118
|
|
|
112
119
|
To make an MCP server available to other systems or agents that support the protocol, register it in the main `Mastra` instance using `mcpServers`.
|
|
113
120
|
|
|
114
|
-
```typescript
|
|
121
|
+
```typescript
|
|
115
122
|
import { Mastra } from "@mastra/core/mastra";
|
|
116
123
|
|
|
117
124
|
import { testMcpServer } from "./mcp/test-mcp-server";
|
|
@@ -126,7 +133,7 @@ export const mastra = new Mastra({
|
|
|
126
133
|
`MCPClient` offers two approaches to retrieving tools from connected servers, suitable for different application architectures:
|
|
127
134
|
|
|
128
135
|
| Feature | Static Configuration (`await mcp.listTools()`) | Dynamic Configuration (`await mcp.listToolsets()`) |
|
|
129
|
-
|
|
|
136
|
+
| ----------------- | ---------------------------------------------- | ---------------------------------------------------- |
|
|
130
137
|
| **Use Case** | Single-user, static config (e.g., CLI tool) | Multi-user, dynamic config (e.g., SaaS app) |
|
|
131
138
|
| **Configuration** | Fixed at agent initialization | Per-request, dynamic |
|
|
132
139
|
| **Credentials** | Shared across all uses | Can vary per user/request |
|
|
@@ -136,11 +143,9 @@ export const mastra = new Mastra({
|
|
|
136
143
|
|
|
137
144
|
Use the `.listTools()` method to fetch tools from all configured MCP servers. This is suitable when configuration (such as API keys) is static and consistent across users or requests. Call it once and pass the result to the `tools` property when defining your agent.
|
|
138
145
|
|
|
139
|
-
> **
|
|
146
|
+
> **Info:** Visit [listTools()](https://mastra.ai/reference/tools/mcp-client) for more information.
|
|
140
147
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
```typescript {6} title="src/mastra/agents/test-agent.ts"
|
|
148
|
+
```typescript
|
|
144
149
|
import { Agent } from "@mastra/core/agent";
|
|
145
150
|
|
|
146
151
|
import { testMcpClient } from "../mcp/test-mcp-client";
|
|
@@ -155,7 +160,7 @@ export const testAgent = new Agent({
|
|
|
155
160
|
|
|
156
161
|
Use the `.listToolsets()` method when tool configuration may vary by request or user, such as in a multi-tenant system where each user provides their own API key. This method returns toolsets that can be passed to the `toolsets` option in the agent's `.generate()` or `.stream()` calls.
|
|
157
162
|
|
|
158
|
-
```typescript
|
|
163
|
+
```typescript
|
|
159
164
|
import { MCPClient } from "@mastra/mcp";
|
|
160
165
|
import { mastra } from "./mastra";
|
|
161
166
|
|
|
@@ -187,132 +192,127 @@ async function handleRequest(userPrompt: string, userApiKey: string) {
|
|
|
187
192
|
}
|
|
188
193
|
```
|
|
189
194
|
|
|
190
|
-
> **
|
|
191
|
-
|
|
192
|
-
Visit [listToolsets()](https://mastra.ai/reference/v1/tools/mcp-client#listtoolsets) for more information.
|
|
195
|
+
> **Info:** Visit [listToolsets()](https://mastra.ai/reference/tools/mcp-client) for more information.
|
|
193
196
|
|
|
194
197
|
## Connecting to an MCP registry
|
|
195
198
|
|
|
196
199
|
MCP servers can be discovered through registries. Here's how to connect to some popular ones using `MCPClient`:
|
|
197
200
|
|
|
198
|
-
|
|
201
|
+
**Klavis AI**:
|
|
199
202
|
|
|
200
|
-
|
|
203
|
+
[Klavis AI](https://klavis.ai) provides hosted, enterprise-authenticated, high-quality MCP servers.
|
|
201
204
|
|
|
202
|
-
|
|
203
|
-
|
|
205
|
+
```typescript
|
|
206
|
+
import { MCPClient } from "@mastra/mcp";
|
|
204
207
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
208
|
+
const mcp = new MCPClient({
|
|
209
|
+
servers: {
|
|
210
|
+
salesforce: {
|
|
211
|
+
url: new URL("https://salesforce-mcp-server.klavis.ai/mcp/?instance_id={private-instance-id}"),
|
|
212
|
+
},
|
|
213
|
+
hubspot: {
|
|
214
|
+
url: new URL("https://hubspot-mcp-server.klavis.ai/mcp/?instance_id={private-instance-id}"),
|
|
215
|
+
},
|
|
216
|
+
},
|
|
217
|
+
});
|
|
218
|
+
```
|
|
216
219
|
|
|
217
|
-
|
|
220
|
+
Klavis AI offers enterprise-grade authentication and security for production deployments.
|
|
218
221
|
|
|
219
|
-
|
|
222
|
+
For more details on how to integrate Mastra with Klavis, check out their [documentation](https://docs.klavis.ai/documentation/ai-platform-integration/mastra).
|
|
220
223
|
|
|
221
|
-
|
|
222
|
-
**mcp-run:**
|
|
224
|
+
**mcp.run**:
|
|
223
225
|
|
|
224
|
-
|
|
226
|
+
[mcp.run](https://www.mcp.run/) provides pre-authenticated, managed MCP servers. Tools are grouped into Profiles, each with a unique, signed URL.
|
|
225
227
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
+
```typescript
|
|
229
|
+
import { MCPClient } from "@mastra/mcp";
|
|
228
230
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
231
|
+
const mcp = new MCPClient({
|
|
232
|
+
servers: {
|
|
233
|
+
marketing: { // Example profile name
|
|
234
|
+
url: new URL(process.env.MCP_RUN_SSE_URL!), // Get URL from mcp.run profile
|
|
235
|
+
},
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
```
|
|
237
239
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
240
|
+
> **Important:** Treat the mcp.run SSE URL like a password. Store it securely, for example, in an environment variable.
|
|
241
|
+
>
|
|
242
|
+
> ```bash
|
|
243
|
+
> MCP_RUN_SSE_URL=https://www.mcp.run/api/mcp/sse?nonce=...
|
|
244
|
+
> ```
|
|
242
245
|
|
|
243
|
-
|
|
244
|
-
**composio:**
|
|
246
|
+
**Composio.dev**:
|
|
245
247
|
|
|
246
|
-
|
|
248
|
+
[Composio.dev](https://composio.dev) offers a registry of [SSE-based MCP servers](https://mcp.composio.dev). You can use the SSE URL generated for tools like Cursor directly.
|
|
247
249
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
+
```typescript
|
|
251
|
+
import { MCPClient } from "@mastra/mcp";
|
|
250
252
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
**smithery:**
|
|
269
|
-
|
|
270
|
-
[Smithery.ai](https://smithery.ai) provides a registry accessible via their CLI.
|
|
271
|
-
|
|
272
|
-
```typescript
|
|
273
|
-
// Unix/Mac
|
|
274
|
-
import { MCPClient } from "@mastra/mcp";
|
|
275
|
-
|
|
276
|
-
const mcp = new MCPClient({
|
|
277
|
-
servers: {
|
|
278
|
-
sequentialThinking: {
|
|
279
|
-
command: "npx",
|
|
280
|
-
args: [
|
|
281
|
-
"-y",
|
|
282
|
-
"@smithery/cli@latest",
|
|
283
|
-
"run",
|
|
284
|
-
"@smithery-ai/server-sequential-thinking",
|
|
285
|
-
"--config",
|
|
286
|
-
"{}",
|
|
287
|
-
],
|
|
288
|
-
},
|
|
289
|
-
},
|
|
290
|
-
});
|
|
291
|
-
```
|
|
292
|
-
|
|
293
|
-
```typescript
|
|
294
|
-
// Windows
|
|
295
|
-
import { MCPClient } from "@mastra/mcp";
|
|
296
|
-
|
|
297
|
-
const mcp = new MCPClient({
|
|
298
|
-
servers: {
|
|
299
|
-
sequentialThinking: {
|
|
300
|
-
command: "npx",
|
|
301
|
-
args: [
|
|
302
|
-
"-y",
|
|
303
|
-
"@smithery/cli@latest",
|
|
304
|
-
"run",
|
|
305
|
-
"@smithery-ai/server-sequential-thinking",
|
|
306
|
-
"--config",
|
|
307
|
-
"{}",
|
|
308
|
-
],
|
|
309
|
-
},
|
|
310
|
-
},
|
|
311
|
-
});
|
|
312
|
-
```
|
|
253
|
+
const mcp = new MCPClient({
|
|
254
|
+
servers: {
|
|
255
|
+
googleSheets: {
|
|
256
|
+
url: new URL("https://mcp.composio.dev/googlesheets/[private-url-path]"),
|
|
257
|
+
},
|
|
258
|
+
gmail: {
|
|
259
|
+
url: new URL("https://mcp.composio.dev/gmail/[private-url-path]"),
|
|
260
|
+
},
|
|
261
|
+
},
|
|
262
|
+
});
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
Authentication with services like Google Sheets often happens interactively through the agent conversation.
|
|
266
|
+
|
|
267
|
+
_Note: Composio URLs are typically tied to a single user account, making them best suited for personal automation rather than multi-tenant applications._
|
|
313
268
|
|
|
314
|
-
|
|
315
|
-
|
|
269
|
+
**Smithery.ai**:
|
|
270
|
+
|
|
271
|
+
[Smithery.ai](https://smithery.ai) provides a registry accessible via their CLI.
|
|
272
|
+
|
|
273
|
+
```typescript
|
|
274
|
+
// Unix/Mac
|
|
275
|
+
import { MCPClient } from "@mastra/mcp";
|
|
276
|
+
|
|
277
|
+
const mcp = new MCPClient({
|
|
278
|
+
servers: {
|
|
279
|
+
sequentialThinking: {
|
|
280
|
+
command: "npx",
|
|
281
|
+
args: [
|
|
282
|
+
"-y",
|
|
283
|
+
"@smithery/cli@latest",
|
|
284
|
+
"run",
|
|
285
|
+
"@smithery-ai/server-sequential-thinking",
|
|
286
|
+
"--config",
|
|
287
|
+
"{}",
|
|
288
|
+
],
|
|
289
|
+
},
|
|
290
|
+
},
|
|
291
|
+
});
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
```typescript
|
|
295
|
+
// Windows
|
|
296
|
+
import { MCPClient } from "@mastra/mcp";
|
|
297
|
+
|
|
298
|
+
const mcp = new MCPClient({
|
|
299
|
+
servers: {
|
|
300
|
+
sequentialThinking: {
|
|
301
|
+
command: "npx",
|
|
302
|
+
args: [
|
|
303
|
+
"-y",
|
|
304
|
+
"@smithery/cli@latest",
|
|
305
|
+
"run",
|
|
306
|
+
"@smithery-ai/server-sequential-thinking",
|
|
307
|
+
"--config",
|
|
308
|
+
"{}",
|
|
309
|
+
],
|
|
310
|
+
},
|
|
311
|
+
},
|
|
312
|
+
});
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
**Ampersand**:
|
|
316
316
|
|
|
317
317
|
[Ampersand](https://withampersand.com?utm_source=mastra-docs) offers an [MCP Server](https://docs.withampersand.com/mcp) that allows you to connect your agent to 150+ integrations with SaaS products like Salesforce, Hubspot, and Zendesk.
|
|
318
318
|
|
|
@@ -363,10 +363,8 @@ export const mcp = new MCPClient({
|
|
|
363
363
|
|
|
364
364
|
As an alternative to MCP, Ampersand's AI SDK also has an adapter for Mastra, so you can [directly import Ampersand tools](https://docs.withampersand.com/ai-sdk#use-with-mastra) for your agent to access.
|
|
365
365
|
|
|
366
|
-
|
|
367
|
-
|
|
368
366
|
## Related
|
|
369
367
|
|
|
370
|
-
- [Using Tools](https://mastra.ai/docs/
|
|
371
|
-
- [MCPClient](https://mastra.ai/reference/
|
|
372
|
-
- [MCPServer](https://mastra.ai/reference/
|
|
368
|
+
- [Using Tools](https://mastra.ai/docs/agents/using-tools)
|
|
369
|
+
- [MCPClient](https://mastra.ai/reference/tools/mcp-client)
|
|
370
|
+
- [MCPServer](https://mastra.ai/reference/tools/mcp-server)
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# Publishing an MCP Server
|
|
2
|
+
|
|
3
|
+
This example guides you through setting up a basic Mastra MCPServer using the stdio transport, building it, and preparing it for publishing to NPM.
|
|
4
|
+
|
|
5
|
+
## Install dependencies
|
|
6
|
+
|
|
7
|
+
Install the necessary packages:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @mastra/mcp @mastra/core tsup
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Setting up an MCP Server
|
|
14
|
+
|
|
15
|
+
1. Create a file for your stdio server, for example, `/src/mastra/stdio.ts`.
|
|
16
|
+
|
|
17
|
+
2. Add the following code to the file. Remember to import your actual Mastra tools and name the server appropriately.
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
#!/usr/bin/env node
|
|
21
|
+
import { MCPServer } from "@mastra/mcp";
|
|
22
|
+
import { weatherTool } from "./tools";
|
|
23
|
+
|
|
24
|
+
const server = new MCPServer({
|
|
25
|
+
name: "my-mcp-server",
|
|
26
|
+
version: "1.0.0",
|
|
27
|
+
tools: { weatherTool },
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
server.startStdio().catch((error) => {
|
|
31
|
+
console.error("Error running MCP server:", error);
|
|
32
|
+
process.exit(1);
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
3. Update your `package.json` to include the `bin` entry pointing to your built server file and a script to build the server with both ESM and CJS outputs.
|
|
37
|
+
|
|
38
|
+
```json
|
|
39
|
+
{
|
|
40
|
+
"bin": "dist/stdio.mjs",
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build:mcp": "tsup src/mastra/stdio.ts --format esm,cjs --no-splitting --dts && echo '#!/usr/bin/env node' | cat - dist/stdio.mjs > temp && mv temp dist/stdio.mjs && chmod +x dist/stdio.mjs"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
The build command generates both ESM (`.mjs`) and CJS (`.cjs`) outputs for maximum compatibility. The shebang (`#!/usr/bin/env node`) is prepended to the ESM artifact to make it directly executable, and the `bin` entry points to this file.
|
|
48
|
+
|
|
49
|
+
4. Run the build command:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
pnpm run build:mcp
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
This will compile your server code into both ESM and CJS formats and make the ESM output file executable. On Unix-like systems, the `chmod +x` step makes the file directly executable. Windows users may need to use WSL or handle execution through Node.js directly.
|
|
56
|
+
|
|
57
|
+
## Publishing to NPM
|
|
58
|
+
|
|
59
|
+
To make your MCP server available for others (or yourself) to use via `npx` or as a dependency, you can publish it to NPM.
|
|
60
|
+
|
|
61
|
+
1. Ensure you have an NPM account and are logged in (`npm login`).
|
|
62
|
+
|
|
63
|
+
2. Make sure your package name in `package.json` is unique and available.
|
|
64
|
+
|
|
65
|
+
3. Run the publish command from your project root after building:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
npm publish --access public
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
For more details on publishing packages, refer to the [NPM documentation](https://docs.npmjs.com/creating-and-publishing-scoped-public-packages).
|
|
72
|
+
|
|
73
|
+
## Using a published MCP Server
|
|
74
|
+
|
|
75
|
+
Once published, your MCP server can be used by an `MCPClient` by specifying the command to run your package. You can also use any other MCP client like Claude desktop, Cursor, or Windsurf.
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import { MCPClient } from "@mastra/mcp";
|
|
79
|
+
|
|
80
|
+
const mcp = new MCPClient({
|
|
81
|
+
servers: {
|
|
82
|
+
// Give this MCP server instance a name
|
|
83
|
+
yourServerName: {
|
|
84
|
+
command: "npx",
|
|
85
|
+
args: ["-y", "@your-org-name/your-package-name@latest"], // Replace with your package name
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// You can then get tools or toolsets from this configuration to use in your agent
|
|
91
|
+
const tools = await mcp.listTools();
|
|
92
|
+
const toolsets = await mcp.listToolsets();
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Note: If you published without an organization scope, the `args` might just be `["-y", "your-package-name@latest"]`.
|