@mastra/mcp 1.0.0-beta.7 → 1.0.0-beta.9

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 CHANGED
@@ -1,5 +1,35 @@
1
1
  # @mastra/mcp
2
2
 
3
+ ## 1.0.0-beta.9
4
+
5
+ ### Patch Changes
6
+
7
+ - dependencies updates: ([#10114](https://github.com/mastra-ai/mastra/pull/10114))
8
+ - Updated dependency [`uuid@^13.0.0` ↗︎](https://www.npmjs.com/package/uuid/v/13.0.0) (from `^11.1.0`, in `dependencies`)
9
+ - Updated dependencies [[`08766f1`](https://github.com/mastra-ai/mastra/commit/08766f15e13ac0692fde2a8bd366c2e16e4321df), [`ae8baf7`](https://github.com/mastra-ai/mastra/commit/ae8baf7d8adcb0ff9dac11880400452bc49b33ff), [`cfabdd4`](https://github.com/mastra-ai/mastra/commit/cfabdd4aae7a726b706942d6836eeca110fb6267), [`a0e437f`](https://github.com/mastra-ai/mastra/commit/a0e437fac561b28ee719e0302d72b2f9b4c138f0), [`bec5efd`](https://github.com/mastra-ai/mastra/commit/bec5efde96653ccae6604e68c696d1bc6c1a0bf5), [`9eedf7d`](https://github.com/mastra-ai/mastra/commit/9eedf7de1d6e0022a2f4e5e9e6fe1ec468f9b43c)]:
10
+ - @mastra/core@1.0.0-beta.21
11
+
12
+ ## 1.0.0-beta.8
13
+
14
+ ### Patch Changes
15
+
16
+ - dependencies updates: ([#10194](https://github.com/mastra-ai/mastra/pull/10194))
17
+ - Updated dependency [`exit-hook@^5.0.1` ↗︎](https://www.npmjs.com/package/exit-hook/v/5.0.1) (from `^4.0.0`, in `dependencies`)
18
+
19
+ - Add embedded documentation support for Mastra packages ([#11472](https://github.com/mastra-ai/mastra/pull/11472))
20
+
21
+ Mastra packages now include embedded documentation in the published npm package under `dist/docs/`. This enables coding agents and AI assistants to understand and use the framework by reading documentation directly from `node_modules`.
22
+
23
+ Each package includes:
24
+ - **SKILL.md** - Entry point explaining the package's purpose and capabilities
25
+ - **SOURCE_MAP.json** - Machine-readable index mapping exports to types and implementation files
26
+ - **Topic folders** - Conceptual documentation organized by feature area
27
+
28
+ Documentation is driven by the `packages` frontmatter field in MDX files, which maps docs to their corresponding packages. CI validation ensures all docs include this field.
29
+
30
+ - Updated dependencies [[`d2d3e22`](https://github.com/mastra-ai/mastra/commit/d2d3e22a419ee243f8812a84e3453dd44365ecb0), [`bc72b52`](https://github.com/mastra-ai/mastra/commit/bc72b529ee4478fe89ecd85a8be47ce0127b82a0), [`05b8bee`](https://github.com/mastra-ai/mastra/commit/05b8bee9e50e6c2a4a2bf210eca25ee212ca24fa), [`c042bd0`](https://github.com/mastra-ai/mastra/commit/c042bd0b743e0e86199d0cb83344ca7690e34a9c), [`940a2b2`](https://github.com/mastra-ai/mastra/commit/940a2b27480626ed7e74f55806dcd2181c1dd0c2), [`e0941c3`](https://github.com/mastra-ai/mastra/commit/e0941c3d7fc75695d5d258e7008fd5d6e650800c), [`0c0580a`](https://github.com/mastra-ai/mastra/commit/0c0580a42f697cd2a7d5973f25bfe7da9055038a), [`28f5f89`](https://github.com/mastra-ai/mastra/commit/28f5f89705f2409921e3c45178796c0e0d0bbb64), [`e601b27`](https://github.com/mastra-ai/mastra/commit/e601b272c70f3a5ecca610373aa6223012704892), [`3d3366f`](https://github.com/mastra-ai/mastra/commit/3d3366f31683e7137d126a3a57174a222c5801fb), [`5a4953f`](https://github.com/mastra-ai/mastra/commit/5a4953f7d25bb15ca31ed16038092a39cb3f98b3), [`eb9e522`](https://github.com/mastra-ai/mastra/commit/eb9e522ce3070a405e5b949b7bf5609ca51d7fe2), [`20e6f19`](https://github.com/mastra-ai/mastra/commit/20e6f1971d51d3ff6dd7accad8aaaae826d540ed), [`4f0b3c6`](https://github.com/mastra-ai/mastra/commit/4f0b3c66f196c06448487f680ccbb614d281e2f7), [`74c4f22`](https://github.com/mastra-ai/mastra/commit/74c4f22ed4c71e72598eacc346ba95cdbc00294f), [`81b6a8f`](https://github.com/mastra-ai/mastra/commit/81b6a8ff79f49a7549d15d66624ac1a0b8f5f971), [`e4d366a`](https://github.com/mastra-ai/mastra/commit/e4d366aeb500371dd4210d6aa8361a4c21d87034), [`a4f010b`](https://github.com/mastra-ai/mastra/commit/a4f010b22e4355a5fdee70a1fe0f6e4a692cc29e), [`73b0bb3`](https://github.com/mastra-ai/mastra/commit/73b0bb394dba7c9482eb467a97ab283dbc0ef4db), [`5627a8c`](https://github.com/mastra-ai/mastra/commit/5627a8c6dc11fe3711b3fa7a6ffd6eb34100a306), [`3ff45d1`](https://github.com/mastra-ai/mastra/commit/3ff45d10e0c80c5335a957ab563da72feb623520), [`251df45`](https://github.com/mastra-ai/mastra/commit/251df4531407dfa46d805feb40ff3fb49769f455), [`f894d14`](https://github.com/mastra-ai/mastra/commit/f894d148946629af7b1f452d65a9cf864cec3765), [`c2b9547`](https://github.com/mastra-ai/mastra/commit/c2b9547bf435f56339f23625a743b2147ab1c7a6), [`580b592`](https://github.com/mastra-ai/mastra/commit/580b5927afc82fe460dfdf9a38a902511b6b7e7f), [`58e3931`](https://github.com/mastra-ai/mastra/commit/58e3931af9baa5921688566210f00fb0c10479fa), [`08bb631`](https://github.com/mastra-ai/mastra/commit/08bb631ae2b14684b2678e3549d0b399a6f0561e), [`4fba91b`](https://github.com/mastra-ai/mastra/commit/4fba91bec7c95911dc28e369437596b152b04cd0), [`12b0cc4`](https://github.com/mastra-ai/mastra/commit/12b0cc4077d886b1a552637dedb70a7ade93528c)]:
31
+ - @mastra/core@1.0.0-beta.20
32
+
3
33
  ## 1.0.0-beta.7
4
34
 
5
35
  ### Patch Changes
@@ -0,0 +1,33 @@
1
+ # @mastra/mcp Documentation
2
+
3
+ > Embedded documentation for coding agents
4
+
5
+ ## Quick Start
6
+
7
+ ```bash
8
+ # Read the skill overview
9
+ cat docs/SKILL.md
10
+
11
+ # Get the source map
12
+ cat docs/SOURCE_MAP.json
13
+
14
+ # Read topic documentation
15
+ cat docs/<topic>/01-overview.md
16
+ ```
17
+
18
+ ## Structure
19
+
20
+ ```
21
+ docs/
22
+ ├── SKILL.md # Entry point
23
+ ├── README.md # This file
24
+ ├── SOURCE_MAP.json # Export index
25
+ ├── mcp/ (2 files)
26
+ ├── tools/ (3 files)
27
+ ├── tools-mcp/ (1 files)
28
+ ```
29
+
30
+ ## Version
31
+
32
+ Package: @mastra/mcp
33
+ Version: 1.0.0-beta.9
@@ -0,0 +1,34 @@
1
+ ---
2
+ name: mastra-mcp-docs
3
+ description: Documentation for @mastra/mcp. Includes links to type definitions and readable implementation code in dist/.
4
+ ---
5
+
6
+ # @mastra/mcp Documentation
7
+
8
+ > **Version**: 1.0.0-beta.9
9
+ > **Package**: @mastra/mcp
10
+
11
+ ## Quick Navigation
12
+
13
+ Use SOURCE_MAP.json to find any export:
14
+
15
+ ```bash
16
+ cat docs/SOURCE_MAP.json
17
+ ```
18
+
19
+ Each export maps to:
20
+ - **types**: `.d.ts` file with JSDoc and API signatures
21
+ - **implementation**: `.js` chunk file with readable source
22
+ - **docs**: Conceptual documentation in `docs/`
23
+
24
+ ## Top Exports
25
+
26
+
27
+
28
+ See SOURCE_MAP.json for the complete list.
29
+
30
+ ## Available Topics
31
+
32
+ - [Mcp](mcp/) - 2 file(s)
33
+ - [Tools](tools/) - 3 file(s)
34
+ - [Tools mcp](tools-mcp/) - 1 file(s)
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": "1.0.0-beta.9",
3
+ "package": "@mastra/mcp",
4
+ "exports": {},
5
+ "modules": {}
6
+ }
@@ -0,0 +1,362 @@
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
+ # MCP Overview
4
+
5
+ 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.
6
+
7
+ Mastra can also be used to author MCP servers, exposing agents, tools, and other structured resources via the MCP interface. These can then be accessed by any system or agent that supports the protocol.
8
+
9
+ Mastra currently supports two MCP classes:
10
+
11
+ 1. **`MCPClient`**: Connects to one or many MCP servers to access their tools, resources, prompts, and handle elicitation requests.
12
+ 2. **`MCPServer`**: Exposes Mastra tools, agents, workflows, prompts, and resources to MCP-compatible clients.
13
+
14
+ ## Getting started
15
+
16
+ To use MCP, install the required dependency:
17
+
18
+ ```bash
19
+ npm install @mastra/mcp@beta
20
+ ```
21
+
22
+ ## Configuring `MCPClient`
23
+
24
+ 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
+
26
+ ```typescript title="src/mastra/mcp/test-mcp-client.ts"
27
+ import { MCPClient } from "@mastra/mcp";
28
+
29
+ export const testMcpClient = new MCPClient({
30
+ id: "test-mcp-client",
31
+ servers: {
32
+ wikipedia: {
33
+ command: "npx",
34
+ args: ["-y", "wikipedia-mcp"],
35
+ },
36
+ weather: {
37
+ url: new URL(
38
+ `https://server.smithery.ai/@smithery-ai/national-weather-service/mcp?api_key=${process.env.SMITHERY_API_KEY}`,
39
+ ),
40
+ },
41
+ },
42
+ });
43
+ ```
44
+
45
+ > **Note:**
46
+
47
+ Visit [MCPClient](https://mastra.ai/reference/v1/tools/mcp-client) for a full list of configuration options.
48
+
49
+ ## Using `MCPClient` with an agent
50
+
51
+ To use tools from an MCP server in an agent, import your `MCPClient` and call `.getTools()` in the `tools` parameter. This loads from the defined MCP servers, making them available to the agent.
52
+
53
+ ```typescript {3,15} title="src/mastra/agents/test-agent.ts"
54
+ import { Agent } from "@mastra/core/agent";
55
+
56
+ import { testMcpClient } from "../mcp/test-mcp-client";
57
+
58
+ export const testAgent = new Agent({
59
+ name: "Test Agent",
60
+ description: "You are a helpful AI assistant",
61
+ instructions: `
62
+ You are a helpful assistant that has access to the following MCP Servers.
63
+ - Wikipedia MCP Server
64
+ - US National Weather Service
65
+
66
+ Answer questions using the information you find using the MCP Servers.`,
67
+ model: "openai/gpt-5.1",
68
+ tools: await testMcpClient.getTools(),
69
+ });
70
+ ```
71
+
72
+ > **Note:**
73
+
74
+ Visit [Agent Class](https://mastra.ai/reference/v1/agents/agent) for a full list of configuration options.
75
+
76
+ ## Configuring `MCPServer`
77
+
78
+ 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.
79
+
80
+ ```typescript title="src/mastra/mcp/test-mcp-server.ts"
81
+ import { MCPServer } from "@mastra/mcp";
82
+
83
+ import { testAgent } from "../agents/test-agent";
84
+ import { testWorkflow } from "../workflows/test-workflow";
85
+ import { testTool } from "../tools/test-tool";
86
+
87
+ export const testMcpServer = new MCPServer({
88
+ id: "test-mcp-server",
89
+ name: "Test Server",
90
+ version: "1.0.0",
91
+ agents: { testAgent },
92
+ tools: { testTool },
93
+ workflows: { testWorkflow },
94
+ });
95
+ ```
96
+
97
+ > **Note:**
98
+
99
+ Visit [MCPServer](https://mastra.ai/reference/v1/tools/mcp-server) for a full list of configuration options.
100
+
101
+ ## Registering an `MCPServer`
102
+
103
+ To make an MCP server available to other systems or agents that support the protocol, register it in the main `Mastra` instance using `mcpServers`.
104
+
105
+ ```typescript title="src/mastra/index.ts"
106
+ import { Mastra } from "@mastra/core/mastra";
107
+
108
+ import { testMcpServer } from "./mcp/test-mcp-server";
109
+
110
+ export const mastra = new Mastra({
111
+ mcpServers: { testMcpServer },
112
+ });
113
+ ```
114
+
115
+ ## Static and dynamic tools
116
+
117
+ `MCPClient` offers two approaches to retrieving tools from connected servers, suitable for different application architectures:
118
+
119
+ | Feature | Static Configuration (`await mcp.getTools()`) | Dynamic Configuration (`await mcp.getToolsets()`) |
120
+ | :---------------- | :-------------------------------------------- | :--------------------------------------------------- |
121
+ | **Use Case** | Single-user, static config (e.g., CLI tool) | Multi-user, dynamic config (e.g., SaaS app) |
122
+ | **Configuration** | Fixed at agent initialization | Per-request, dynamic |
123
+ | **Credentials** | Shared across all uses | Can vary per user/request |
124
+ | **Agent Setup** | Tools added in `Agent` constructor | Tools passed in `.generate()` or `.stream()` options |
125
+
126
+ ### Static tools
127
+
128
+ Use the `.getTools()` 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.
129
+
130
+ > **Note:**
131
+
132
+ Visit [getTools()](https://mastra.ai/reference/v1/tools/mcp-client#gettools) for more information.
133
+
134
+ ```typescript {6} title="src/mastra/agents/test-agent.ts"
135
+ import { Agent } from "@mastra/core/agent";
136
+
137
+ import { testMcpClient } from "../mcp/test-mcp-client";
138
+
139
+ export const testAgent = new Agent({
140
+ tools: await testMcpClient.getTools(),
141
+ });
142
+ ```
143
+
144
+ ### Dynamic tools
145
+
146
+ Use the `.getToolsets()` 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.
147
+
148
+ ```typescript {5-16,21}
149
+ import { MCPClient } from "@mastra/mcp";
150
+ import { mastra } from "./mastra";
151
+
152
+ async function handleRequest(userPrompt: string, userApiKey: string) {
153
+ const userMcp = new MCPClient({
154
+ servers: {
155
+ weather: {
156
+ url: new URL("http://localhost:8080/mcp"),
157
+ requestInit: {
158
+ headers: {
159
+ Authorization: `Bearer ${userApiKey}`,
160
+ },
161
+ },
162
+ },
163
+ },
164
+ });
165
+
166
+ const agent = mastra.getAgent("testAgent");
167
+
168
+ const response = await agent.generate(userPrompt, {
169
+ toolsets: await userMcp.getToolsets(),
170
+ });
171
+
172
+ await userMcp.disconnect();
173
+
174
+ return Response.json({
175
+ data: response.text,
176
+ });
177
+ }
178
+ ```
179
+
180
+ > **Note:**
181
+
182
+ Visit [getToolsets()](https://mastra.ai/reference/v1/tools/mcp-client#gettoolsets) for more information.
183
+
184
+ ## Connecting to an MCP registry
185
+
186
+ MCP servers can be discovered through registries. Here's how to connect to some popular ones using `MCPClient`:
187
+
188
+ **klavis:**
189
+
190
+ [Klavis AI](https://klavis.ai) provides hosted, enterprise-authenticated, high-quality MCP servers.
191
+
192
+ ```typescript
193
+ import { MCPClient } from "@mastra/mcp";
194
+
195
+ const mcp = new MCPClient({
196
+ servers: {
197
+ salesforce: {
198
+ url: new URL("https://salesforce-mcp-server.klavis.ai/mcp/?instance_id={private-instance-id}"),
199
+ },
200
+ hubspot: {
201
+ url: new URL("https://hubspot-mcp-server.klavis.ai/mcp/?instance_id={private-instance-id}"),
202
+ },
203
+ },
204
+ });
205
+ ```
206
+
207
+ Klavis AI offers enterprise-grade authentication and security for production deployments.
208
+
209
+ For more details on how to integrate Mastra with Klavis, check out their [documentation](https://docs.klavis.ai/documentation/ai-platform-integration/mastra).
210
+
211
+
212
+ **mcp-run:**
213
+
214
+ [mcp.run](https://www.mcp.run/) provides pre-authenticated, managed MCP servers. Tools are grouped into Profiles, each with a unique, signed URL.
215
+
216
+ ```typescript
217
+ import { MCPClient } from "@mastra/mcp";
218
+
219
+ const mcp = new MCPClient({
220
+ servers: {
221
+ marketing: { // Example profile name
222
+ url: new URL(process.env.MCP_RUN_SSE_URL!), // Get URL from mcp.run profile
223
+ },
224
+ },
225
+ });
226
+ ```
227
+
228
+ > **Important:** Treat the mcp.run SSE URL like a password. Store it securely, for example, in an environment variable.
229
+ > ```bash title=".env"
230
+ > MCP_RUN_SSE_URL=https://www.mcp.run/api/mcp/sse?nonce=...
231
+ > ```
232
+
233
+
234
+ **composio:**
235
+
236
+ [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.
237
+
238
+ ```typescript
239
+ import { MCPClient } from "@mastra/mcp";
240
+
241
+ const mcp = new MCPClient({
242
+ servers: {
243
+ googleSheets: {
244
+ url: new URL("https://mcp.composio.dev/googlesheets/[private-url-path]"),
245
+ },
246
+ gmail: {
247
+ url: new URL("https://mcp.composio.dev/gmail/[private-url-path]"),
248
+ },
249
+ },
250
+ });
251
+ ```
252
+
253
+ Authentication with services like Google Sheets often happens interactively through the agent conversation.
254
+
255
+ *Note: Composio URLs are typically tied to a single user account, making them best suited for personal automation rather than multi-tenant applications.*
256
+
257
+
258
+ **smithery:**
259
+
260
+ [Smithery.ai](https://smithery.ai) provides a registry accessible via their CLI.
261
+
262
+ ```typescript
263
+ // Unix/Mac
264
+ import { MCPClient } from "@mastra/mcp";
265
+
266
+ const mcp = new MCPClient({
267
+ servers: {
268
+ sequentialThinking: {
269
+ command: "npx",
270
+ args: [
271
+ "-y",
272
+ "@smithery/cli@latest",
273
+ "run",
274
+ "@smithery-ai/server-sequential-thinking",
275
+ "--config",
276
+ "{}",
277
+ ],
278
+ },
279
+ },
280
+ });
281
+ ```
282
+
283
+ ```typescript
284
+ // Windows
285
+ import { MCPClient } from "@mastra/mcp";
286
+
287
+ const mcp = new MCPClient({
288
+ servers: {
289
+ sequentialThinking: {
290
+ command: "npx",
291
+ args: [
292
+ "-y",
293
+ "@smithery/cli@latest",
294
+ "run",
295
+ "@smithery-ai/server-sequential-thinking",
296
+ "--config",
297
+ "{}",
298
+ ],
299
+ },
300
+ },
301
+ });
302
+ ```
303
+
304
+
305
+ **ampersand:**
306
+
307
+ [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.
308
+
309
+ ```typescript
310
+ // MCPClient with Ampersand MCP Server using SSE
311
+ export const mcp = new MCPClient({
312
+ servers: {
313
+ "@amp-labs/mcp-server": {
314
+ url: `https://mcp.withampersand.com/v1/sse?${new URLSearchParams({
315
+ apiKey: process.env.AMPERSAND_API_KEY,
316
+ project: process.env.AMPERSAND_PROJECT_ID,
317
+ integrationName: process.env.AMPERSAND_INTEGRATION_NAME,
318
+ groupRef: process.env.AMPERSAND_GROUP_REF,
319
+ })}`,
320
+ },
321
+ },
322
+ });
323
+ ```
324
+
325
+ ```typescript
326
+ // If you prefer to run the MCP server locally:
327
+ import { MCPClient } from "@mastra/mcp";
328
+
329
+ // MCPClient with Ampersand MCP Server using stdio transport
330
+ export const mcp = new MCPClient({
331
+ servers: {
332
+ "@amp-labs/mcp-server": {
333
+ command: "npx",
334
+ args: [
335
+ "-y",
336
+ "@amp-labs/mcp-server@latest",
337
+ "--transport",
338
+ "stdio",
339
+ "--project",
340
+ process.env.AMPERSAND_PROJECT_ID,
341
+ "--integrationName",
342
+ process.env.AMPERSAND_INTEGRATION_NAME,
343
+ "--groupRef",
344
+ process.env.AMPERSAND_GROUP_REF, // optional
345
+ ],
346
+ env: {
347
+ AMPERSAND_API_KEY: process.env.AMPERSAND_API_KEY,
348
+ },
349
+ },
350
+ },
351
+ });
352
+ ```
353
+
354
+ 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.
355
+
356
+
357
+
358
+ ## Related
359
+
360
+ - [Using Tools](https://mastra.ai/docs/v1/agents/using-tools)
361
+ - [MCPClient](https://mastra.ai/reference/v1/tools/mcp-client)
362
+ - [MCPServer](https://mastra.ai/reference/v1/tools/mcp-server)
@@ -0,0 +1,111 @@
1
+ > Guide to setting up and building a Mastra MCP server using the stdio transport, and publishing it to NPM.
2
+
3
+ # Publishing an MCP Server
4
+
5
+ This example guides you through setting up a basic Mastra MCPServer using the stdio transport, building it, and preparing it for publishing to NPM.
6
+
7
+ ## Install dependencies
8
+
9
+ Install the necessary packages:
10
+
11
+ ```bash
12
+ pnpm add @mastra/mcp @mastra/core tsup
13
+ ```
14
+
15
+ ## Setting up an MCP Server
16
+
17
+ ### Step
18
+
19
+ Create a file for your stdio server, for example, `/src/mastra/stdio.ts`.
20
+
21
+ ### Step
22
+
23
+ Add the following code to the file. Remember to import your actual Mastra tools and name the server appropriately.
24
+
25
+ ```typescript title="src/mastra/stdio.ts"
26
+ #!/usr/bin/env node
27
+ import { MCPServer } from "@mastra/mcp";
28
+ import { weatherTool } from "./tools";
29
+
30
+ const server = new MCPServer({
31
+ name: "my-mcp-server",
32
+ version: "1.0.0",
33
+ tools: { weatherTool },
34
+ });
35
+
36
+ server.startStdio().catch((error) => {
37
+ console.error("Error running MCP server:", error);
38
+ process.exit(1);
39
+ });
40
+ ```
41
+
42
+ ### Step
43
+
44
+ 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.
45
+
46
+ ```json title="package.json"
47
+ {
48
+ "bin": "dist/stdio.mjs",
49
+ "scripts": {
50
+ "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"
51
+ }
52
+ }
53
+ ```
54
+
55
+ 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.
56
+
57
+ ### Step
58
+
59
+ Run the build command:
60
+
61
+ ```bash
62
+ pnpm run build:mcp
63
+ ```
64
+
65
+ 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.
66
+
67
+ ## Publishing to NPM
68
+
69
+ To make your MCP server available for others (or yourself) to use via `npx` or as a dependency, you can publish it to NPM.
70
+
71
+ ### Step
72
+
73
+ Ensure you have an NPM account and are logged in (`npm login`).
74
+
75
+ ### Step
76
+
77
+ Make sure your package name in `package.json` is unique and available.
78
+
79
+ ### Step
80
+
81
+ Run the publish command from your project root after building:
82
+
83
+ ```bash
84
+ npm publish --access public
85
+ ```
86
+
87
+ For more details on publishing packages, refer to the [NPM documentation](https://docs.npmjs.com/creating-and-publishing-scoped-public-packages).
88
+
89
+ ## Using a published MCP Server
90
+
91
+ 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.
92
+
93
+ ```typescript
94
+ import { MCPClient } from "@mastra/mcp";
95
+
96
+ const mcp = new MCPClient({
97
+ servers: {
98
+ // Give this MCP server instance a name
99
+ yourServerName: {
100
+ command: "npx",
101
+ args: ["-y", "@your-org-name/your-package-name@latest"], // Replace with your package name
102
+ },
103
+ },
104
+ });
105
+
106
+ // You can then get tools or toolsets from this configuration to use in your agent
107
+ const tools = await mcp.getTools();
108
+ const toolsets = await mcp.getToolsets();
109
+ ```
110
+
111
+ Note: If you published without an organization scope, the `args` might just be `["-y", "your-package-name@latest"]`.