@mastra/mcp-docs-server 1.1.1-alpha.2 → 1.1.2-alpha.0
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/.docs/docs/deployment/cloud-providers.md +1 -1
- package/.docs/docs/deployment/overview.md +1 -1
- package/.docs/docs/deployment/studio.md +234 -0
- package/.docs/docs/memory/observational-memory.md +86 -11
- package/.docs/docs/streaming/events.md +23 -0
- package/.docs/docs/workspace/filesystem.md +72 -1
- package/.docs/docs/workspace/overview.md +95 -12
- package/.docs/docs/workspace/sandbox.md +2 -0
- package/.docs/guides/agent-frameworks/ai-sdk.md +6 -2
- package/.docs/guides/deployment/cloudflare.md +99 -0
- package/.docs/guides/guide/code-review-bot.md +221 -0
- package/.docs/guides/guide/dev-assistant.md +304 -0
- package/.docs/guides/guide/docs-manager.md +238 -0
- package/.docs/models/gateways/openrouter.md +6 -3
- package/.docs/models/index.md +1 -1
- package/.docs/models/providers/baseten.md +2 -1
- package/.docs/models/providers/cerebras.md +2 -1
- package/.docs/models/providers/fireworks-ai.md +2 -1
- package/.docs/models/providers/friendli.md +3 -2
- package/.docs/models/providers/huggingface.md +3 -2
- package/.docs/models/providers/jiekou.md +4 -2
- package/.docs/models/providers/minimax-cn-coding-plan.md +3 -2
- package/.docs/models/providers/minimax-cn.md +3 -2
- package/.docs/models/providers/minimax-coding-plan.md +3 -2
- package/.docs/models/providers/minimax.md +3 -2
- package/.docs/models/providers/nano-gpt.md +12 -4
- package/.docs/models/providers/novita-ai.md +4 -2
- package/.docs/models/providers/ollama-cloud.md +3 -1
- package/.docs/models/providers/openai.md +15 -14
- package/.docs/models/providers/opencode.md +31 -32
- package/.docs/models/providers/stackit.md +78 -0
- package/.docs/models/providers/synthetic.md +1 -1
- package/.docs/models/providers/zai-coding-plan.md +3 -2
- package/.docs/models/providers/zai.md +3 -2
- package/.docs/models/providers/zhipuai-coding-plan.md +3 -2
- package/.docs/models/providers/zhipuai.md +3 -2
- package/.docs/models/providers.md +1 -0
- package/.docs/reference/ai-sdk/with-mastra.md +1 -1
- package/.docs/reference/cli/mastra.md +1 -1
- package/.docs/reference/deployer/cloudflare.md +35 -12
- package/.docs/reference/index.md +3 -0
- package/.docs/reference/memory/observational-memory.md +318 -9
- package/.docs/reference/streaming/workflows/stream.md +1 -0
- package/.docs/reference/workflows/workflow-methods/foreach.md +30 -0
- package/.docs/reference/workspace/e2b-sandbox.md +299 -0
- package/.docs/reference/workspace/gcs-filesystem.md +170 -0
- package/.docs/reference/workspace/s3-filesystem.md +169 -0
- package/CHANGELOG.md +15 -0
- package/package.json +6 -6
- package/.docs/guides/deployment/cloudflare-deployer.md +0 -102
|
@@ -19,6 +19,7 @@ When you assign a workspace with a sandbox to an agent, Mastra automatically inc
|
|
|
19
19
|
Available providers:
|
|
20
20
|
|
|
21
21
|
- [`LocalSandbox`](https://mastra.ai/reference/workspace/local-sandbox) - Executes commands on the local machine
|
|
22
|
+
- [`E2BSandbox`](https://mastra.ai/reference/workspace/e2b-sandbox) - Executes commands in isolated E2B cloud sandboxes
|
|
22
23
|
|
|
23
24
|
## Basic usage
|
|
24
25
|
|
|
@@ -57,5 +58,6 @@ When you configure a sandbox on a workspace, agents receive the `execute_command
|
|
|
57
58
|
## Related
|
|
58
59
|
|
|
59
60
|
- [`LocalSandbox` reference](https://mastra.ai/reference/workspace/local-sandbox)
|
|
61
|
+
- [`E2BSandbox` reference](https://mastra.ai/reference/workspace/e2b-sandbox)
|
|
60
62
|
- [Workspace overview](https://mastra.ai/docs/workspace/overview)
|
|
61
63
|
- [Filesystem](https://mastra.ai/docs/workspace/filesystem)
|
|
@@ -83,9 +83,11 @@ const storage = new LibSQLStore({
|
|
|
83
83
|
});
|
|
84
84
|
await storage.init();
|
|
85
85
|
|
|
86
|
+
const memoryStorage = await storage.getStore('memory');
|
|
87
|
+
|
|
86
88
|
const model = withMastra(openai('gpt-4o'), {
|
|
87
89
|
memory: {
|
|
88
|
-
storage
|
|
90
|
+
storage: memoryStorage!,
|
|
89
91
|
threadId: 'user-thread-123',
|
|
90
92
|
resourceId: 'user-123',
|
|
91
93
|
lastMessages: 10,
|
|
@@ -111,11 +113,13 @@ import { LibSQLStore } from '@mastra/libsql';
|
|
|
111
113
|
const storage = new LibSQLStore({ id: 'my-app', url: 'file:./data.db' });
|
|
112
114
|
await storage.init();
|
|
113
115
|
|
|
116
|
+
const memoryStorage = await storage.getStore('memory');
|
|
117
|
+
|
|
114
118
|
const model = withMastra(openai('gpt-4o'), {
|
|
115
119
|
inputProcessors: [myGuardProcessor],
|
|
116
120
|
outputProcessors: [myLoggingProcessor],
|
|
117
121
|
memory: {
|
|
118
|
-
storage
|
|
122
|
+
storage: memoryStorage!,
|
|
119
123
|
threadId: 'thread-123',
|
|
120
124
|
resourceId: 'user-123',
|
|
121
125
|
lastMessages: 10,
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# Deploy Mastra to Cloudflare
|
|
2
|
+
|
|
3
|
+
Use `@mastra/deployer-cloudflare` to deploy your Mastra server to Cloudflare Workers. The deployer bundles your code and generates a `wrangler.jsonc` file conforming to Cloudflare's [wrangler configuration](https://developers.cloudflare.com/workers/wrangler/configuration/), ready to deploy with no additional configuration.
|
|
4
|
+
|
|
5
|
+
> **Info:** If you're using a [server adapter](https://mastra.ai/docs/server/server-adapters) or [web framework](https://mastra.ai/docs/deployment/web-framework), deploy the way you normally would for that framework.
|
|
6
|
+
|
|
7
|
+
## Before you begin
|
|
8
|
+
|
|
9
|
+
You'll need a [Mastra application](https://mastra.ai/guides/getting-started/quickstart) and a [Cloudflare](https://cloudflare.com/) account.
|
|
10
|
+
|
|
11
|
+
Cloudflare Workers use an ephemeral filesystem, so any storage you configure (including observability storage) must be hosted externally. If you're using [LibSQLStore](https://mastra.ai/reference/storage/libsql) with a file URL, switch to a remotely hosted database.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
Add the `@mastra/deployer-cloudflare` package to your project:
|
|
16
|
+
|
|
17
|
+
**npm**:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @mastra/deployer-cloudflare@latest
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**pnpm**:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pnpm add @mastra/deployer-cloudflare@latest
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**Yarn**:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
yarn add @mastra/deployer-cloudflare@latest
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
**Bun**:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
bun add @mastra/deployer-cloudflare@latest
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Import [`CloudflareDeployer`](https://mastra.ai/reference/deployer/cloudflare) and set it as the deployer in your Mastra configuration:
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { Mastra } from "@mastra/core";
|
|
45
|
+
import { CloudflareDeployer } from "@mastra/deployer-cloudflare";
|
|
46
|
+
|
|
47
|
+
export const mastra = new Mastra({
|
|
48
|
+
deployer: new CloudflareDeployer({
|
|
49
|
+
name: "your-project-name",
|
|
50
|
+
vars: {
|
|
51
|
+
NODE_ENV: "production",
|
|
52
|
+
},
|
|
53
|
+
}),
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
In order to test your Cloudflare Worker locally, also install the [`wrangler` CLI](https://developers.cloudflare.com/workers/wrangler/install-and-update/):
|
|
58
|
+
|
|
59
|
+
**npm**:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
npm install -D wrangler
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
**pnpm**:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
pnpm add -D wrangler
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**Yarn**:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
yarn add --dev wrangler
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
**Bun**:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
bun add --dev wrangler
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Usage
|
|
84
|
+
|
|
85
|
+
After setting up your project, push it to your remote Git provider of choice (e.g. GitHub).
|
|
86
|
+
|
|
87
|
+
1. Connect your repository to Cloudflare. On the "Workers & Pages" dashboard, select **Create application** and choose your Git provider in the next step. Continue with the setup process and select the repository you want to deploy.
|
|
88
|
+
|
|
89
|
+
> **Note:** Remember to set your environment variables needed to run your application (e.g. your [model provider](https://mastra.ai/models/providers) API key).
|
|
90
|
+
|
|
91
|
+
2. Once you're ready, click the **Deploy** button and wait for the first deployment to complete.
|
|
92
|
+
|
|
93
|
+
3. Try out your newly deployed function by going to `https://<your-project-name>.<slug>.workers.dev/api/agents`. You should get a JSON response listing all available agents.
|
|
94
|
+
|
|
95
|
+
Since the [Mastra server](https://mastra.ai/docs/server/mastra-server) prefixes every API endpoint with `/api`, you have to add it to your URLs when making requests.
|
|
96
|
+
|
|
97
|
+
4. You can now call your Mastra endpoints over HTTP.
|
|
98
|
+
|
|
99
|
+
> **Note:** Set up [authentication](https://mastra.ai/docs/server/auth) before exposing your endpoints publicly.
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
# Building a Code Review Bot
|
|
2
|
+
|
|
3
|
+
In this guide, you'll build a code review bot that automatically reviews pull requests using workspace skills. The bot loads coding standards from skill files and provides structured feedback. You'll learn how to create a workspace with a skills directory, define an [Agent Skill](https://agentskills.io) with review instructions and reference files, and connect it to an agent that performs automated reviews.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
- Node.js `v22.13.0` or later installed
|
|
8
|
+
- An API key from a supported [Model Provider](https://mastra.ai/models)
|
|
9
|
+
- An existing Mastra project (Follow the [installation guide](https://mastra.ai/guides/getting-started/quickstart) to set up a new project)
|
|
10
|
+
|
|
11
|
+
## Create the workspace
|
|
12
|
+
|
|
13
|
+
In your `src/mastra/index.ts` file, import the [`Workspace`](https://mastra.ai/reference/workspace/workspace-class) and [`LocalFilesystem`](https://mastra.ai/reference/workspace/local-filesystem) classes. On the `Workspace` instance, configure the `skills` option to point to a skills directory. The `skills` directory will live inside the filesystem's `basePath`.
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { Mastra } from '@mastra/core';
|
|
17
|
+
import { resolve } from 'node:path';
|
|
18
|
+
import { Workspace, LocalFilesystem } from '@mastra/core/workspace';
|
|
19
|
+
|
|
20
|
+
const workspace = new Workspace({
|
|
21
|
+
filesystem: new LocalFilesystem({
|
|
22
|
+
basePath: resolve(import.meta.dirname, '../../workspace'),
|
|
23
|
+
}),
|
|
24
|
+
skills: ['/skills'],
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export const mastra = new Mastra({
|
|
28
|
+
workspace,
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
At the root of your project, create a new folder called `workspace`. Inside that, create a `skills` folder. This is where you'll define the code standards skill in the next step.
|
|
33
|
+
|
|
34
|
+
## Create the code standards skill
|
|
35
|
+
|
|
36
|
+
Skills are structured directories containing a `SKILL.md` file with instructions for the agent. The code standards skill defines the review process and references a style guide.
|
|
37
|
+
|
|
38
|
+
Inside `workspace/skills`, create a new folder called `code-standards`. Create a file called `SKILL.md` and add the review instructions.
|
|
39
|
+
|
|
40
|
+
```markdown
|
|
41
|
+
---
|
|
42
|
+
name: code-standards
|
|
43
|
+
description: Automated code review standards and checks
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
# Code Review Standards
|
|
47
|
+
|
|
48
|
+
Review code systematically using these steps:
|
|
49
|
+
|
|
50
|
+
1. **Critical Issues**: Security vulnerabilities, memory leaks, logic bugs, missing error handling
|
|
51
|
+
2. **Code Quality**: Functions over 50 lines, code duplication, confusing names, missing types
|
|
52
|
+
3. **Style Guide**: Check references/style-guide.md for naming and organization
|
|
53
|
+
4. **Linting**: Flag common issues like use of `var`, leftover `console.log` statements, and `debugger` statements
|
|
54
|
+
|
|
55
|
+
Provide feedback in this format:
|
|
56
|
+
|
|
57
|
+
**Summary**: One sentence overview
|
|
58
|
+
|
|
59
|
+
**Critical Issues**: List with line numbers
|
|
60
|
+
|
|
61
|
+
**Suggestions**: Improvements that would help
|
|
62
|
+
|
|
63
|
+
**Positive Notes**: What the code does well
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Inside `workspace/skills/code-standards`, create a `references` folder to hold reference materials for the skill. Author a style guide file that outlines the project's coding conventions with the file name `style-guide.md`.
|
|
67
|
+
|
|
68
|
+
````markdown
|
|
69
|
+
# Style Guide
|
|
70
|
+
|
|
71
|
+
## Naming
|
|
72
|
+
|
|
73
|
+
- Variables/Functions: `camelCase`
|
|
74
|
+
- Constants: `UPPER_SNAKE_CASE`
|
|
75
|
+
- Files: `kebab-case.ts`
|
|
76
|
+
- Booleans: Start with `is`, `has`, `should`
|
|
77
|
+
|
|
78
|
+
## Code Organization
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
// 1. Imports
|
|
82
|
+
import { foo } from 'bar';
|
|
83
|
+
|
|
84
|
+
// 2. Constants
|
|
85
|
+
const MAX_SIZE = 100;
|
|
86
|
+
|
|
87
|
+
// 3. Types
|
|
88
|
+
interface User { id: string; }
|
|
89
|
+
|
|
90
|
+
// 4. Functions
|
|
91
|
+
function doSomething() {}
|
|
92
|
+
|
|
93
|
+
// 5. Exports
|
|
94
|
+
export { doSomething };
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Error Handling
|
|
98
|
+
|
|
99
|
+
Always handle errors explicitly - never silently catch.
|
|
100
|
+
|
|
101
|
+
## Comments
|
|
102
|
+
|
|
103
|
+
Write "why" not "what".
|
|
104
|
+
````
|
|
105
|
+
|
|
106
|
+
## Create the review agent
|
|
107
|
+
|
|
108
|
+
Now it's time to create the code review bot agent that uses the code-standards skill. Create a new file at `src/mastra/agents/code-reviewer.ts` and define the agent:
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
import { Agent } from '@mastra/core/agent';
|
|
112
|
+
|
|
113
|
+
export const codeReviewer = new Agent({
|
|
114
|
+
id: 'code-reviewer',
|
|
115
|
+
name: 'Code Review Bot',
|
|
116
|
+
instructions: `You are an automated code reviewer.
|
|
117
|
+
|
|
118
|
+
When asked to review code:
|
|
119
|
+
1. Activate the 'code-standards' skill
|
|
120
|
+
2. Follow the review process from the skill
|
|
121
|
+
3. Check against the style guide in skill references
|
|
122
|
+
4. Be constructive and specific with line numbers`,
|
|
123
|
+
model: 'openai/gpt-4o',
|
|
124
|
+
});
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Define the agent by importing it inside `src/mastra/index.ts` and registering it with the `Mastra` instance:
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
import { Mastra } from '@mastra/core';
|
|
131
|
+
import { resolve } from 'node:path';
|
|
132
|
+
import { Workspace, LocalFilesystem } from '@mastra/core/workspace';
|
|
133
|
+
import { codeReviewer } from './agents/code-reviewer';
|
|
134
|
+
|
|
135
|
+
const workspace = new Workspace({
|
|
136
|
+
filesystem: new LocalFilesystem({
|
|
137
|
+
basePath: resolve(import.meta.dirname, '../../workspace'),
|
|
138
|
+
}),
|
|
139
|
+
skills: ['/skills'],
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
export const mastra = new Mastra({
|
|
143
|
+
workspace,
|
|
144
|
+
agents: { codeReviewer },
|
|
145
|
+
});
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Test the bot
|
|
149
|
+
|
|
150
|
+
Start Mastra Studio and interact with the code review bot to see it in action.
|
|
151
|
+
|
|
152
|
+
**npm**:
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
npm run dev
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
**pnpm**:
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
pnpm run dev
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
**Yarn**:
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
yarn dev
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
**Bun**:
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
bun run dev
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Open [localhost:4111](http://localhost:4111) and navigate to the code reviewer agent.
|
|
177
|
+
|
|
178
|
+
Inside the chat input, provide a code snippet for review, such as:
|
|
179
|
+
|
|
180
|
+
```text
|
|
181
|
+
Review this code:
|
|
182
|
+
|
|
183
|
+
function getData(id) {
|
|
184
|
+
var result = fetch('/api/data/' + id);
|
|
185
|
+
console.log(result);
|
|
186
|
+
return result;
|
|
187
|
+
}
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
The bot should activate the `code-standards` skill and provide structured feedback. Since agent responses are non-deterministic, your output may vary, but you should see something similar to:
|
|
191
|
+
|
|
192
|
+
```md
|
|
193
|
+
**Summary**: Function has several issues with variable declaration,
|
|
194
|
+
debugging statements, and missing error handling.
|
|
195
|
+
|
|
196
|
+
**Critical Issues**:
|
|
197
|
+
- Missing error handling for fetch (line 2)
|
|
198
|
+
- No async/await for asynchronous operation (line 2)
|
|
199
|
+
|
|
200
|
+
**Suggestions**:
|
|
201
|
+
- Use const instead of var (line 2)
|
|
202
|
+
- Remove console.log before committing (line 3)
|
|
203
|
+
- Add TypeScript type for id parameter
|
|
204
|
+
- Use template literals instead of concatenation
|
|
205
|
+
|
|
206
|
+
**Positive Notes**:
|
|
207
|
+
- Function name is clear and descriptive
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## Next steps
|
|
211
|
+
|
|
212
|
+
You can extend this bot to:
|
|
213
|
+
|
|
214
|
+
- Add skills for different languages or frameworks
|
|
215
|
+
- Create skills for security checks and performance reviews
|
|
216
|
+
- Integrate with GitHub Actions for automatic PR reviews
|
|
217
|
+
- Build a PR comment bot that leaves inline feedback
|
|
218
|
+
|
|
219
|
+
Learn more:
|
|
220
|
+
|
|
221
|
+
- [Agent Skills spec](https://agentskills.io)
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
# Building a Dev Assistant
|
|
2
|
+
|
|
3
|
+
In this guide, you'll build a complete development assistant that combines all workspace features:
|
|
4
|
+
|
|
5
|
+
- [Filesystem](https://mastra.ai/docs/workspace/filesystem) for file management
|
|
6
|
+
- [Sandbox](https://mastra.ai/docs/workspace/sandbox) for code execution
|
|
7
|
+
- [Skills](https://mastra.ai/docs/workspace/skills) for coding standards
|
|
8
|
+
- [Search](https://mastra.ai/docs/workspace/search) for finding examples
|
|
9
|
+
|
|
10
|
+
You'll set up a workspace with a sample project, add coding standards as a skill, and create an agent that writes code following TDD practices. By the end, you'll have an agent that can read existing code, write new implementations, run tests in a sandbox, and iterate based on results.
|
|
11
|
+
|
|
12
|
+
## Prerequisites
|
|
13
|
+
|
|
14
|
+
- Node.js `v22.13.0` or later installed
|
|
15
|
+
- An API key from a supported [Model Provider](https://mastra.ai/models)
|
|
16
|
+
- An existing Mastra project (Follow the [installation guide](https://mastra.ai/guides/getting-started/quickstart) to set up a new project)
|
|
17
|
+
|
|
18
|
+
### Install vitest
|
|
19
|
+
|
|
20
|
+
The dev assistant will use [Vitest](https://vitest.dev/) to run tests inside the workspace sandbox. Install it as a dev dependency in your project:
|
|
21
|
+
|
|
22
|
+
**npm**:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install -D vitest
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
**pnpm**:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pnpm add -D vitest
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
**Yarn**:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
yarn add --dev vitest
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
**Bun**:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
bun add --dev vitest
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Set up the workspace
|
|
47
|
+
|
|
48
|
+
The workspace uses a local filesystem to manage documentation files. The agent reads and writes files within the workspace directory. In your `src/mastra/index.ts` file, import the [`Workspace`](https://mastra.ai/reference/workspace/workspace-class), [`LocalFilesystem`](https://mastra.ai/reference/workspace/local-filesystem), and [`LocalSandbox`](https://mastra.ai/reference/workspace/local-sandbox) classes.
|
|
49
|
+
|
|
50
|
+
Additionally, enable BM25 search indexing and load skills from the `skills` directory.
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import { Mastra } from '@mastra/core';
|
|
54
|
+
import { resolve } from 'node:path';
|
|
55
|
+
import { Workspace, LocalFilesystem, LocalSandbox } from '@mastra/core/workspace';
|
|
56
|
+
|
|
57
|
+
const workspace = new Workspace({
|
|
58
|
+
filesystem: new LocalFilesystem({ basePath: resolve(import.meta.dirname, '../../workspace') }),
|
|
59
|
+
sandbox: new LocalSandbox({ workingDirectory: resolve(import.meta.dirname, '../../workspace') }),
|
|
60
|
+
skills: ['/skills'],
|
|
61
|
+
bm25: true,
|
|
62
|
+
autoIndexPaths: ['/docs', '/src'],
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
export const mastra = new Mastra({
|
|
66
|
+
workspace,
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
At the root of your project, create a new folder called `workspace`. This is where all files will be stored and managed by the agent.
|
|
71
|
+
|
|
72
|
+
## Add sample project files
|
|
73
|
+
|
|
74
|
+
The workspace uses the following folder structure:
|
|
75
|
+
|
|
76
|
+
- `workspace/src/`: Source code for the sample project
|
|
77
|
+
- `workspace/tests/`: Test files
|
|
78
|
+
- `workspace/docs/`: Project documentation
|
|
79
|
+
- `workspace/skills/`: Coding standards and guidelines as [Agent Skills](https://agentskills.io)
|
|
80
|
+
|
|
81
|
+
Get started by creating a `workspace/src/utils/string-helpers.ts` file with some utility functions, and a corresponding test file in `workspace/tests/string-helpers.test.ts`.
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
export function capitalize(str: string): string {
|
|
85
|
+
if (!str) return str;
|
|
86
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function slugify(str: string): string {
|
|
90
|
+
return str
|
|
91
|
+
.toLowerCase()
|
|
92
|
+
.replace(/[^\w\s-]/g, '')
|
|
93
|
+
.replace(/\s+/g, '-');
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
import { describe, it, expect } from 'vitest';
|
|
99
|
+
import { capitalize, slugify } from '../src/utils/string-helpers';
|
|
100
|
+
|
|
101
|
+
describe('String Helpers', () => {
|
|
102
|
+
describe('capitalize', () => {
|
|
103
|
+
it('capitalizes first letter', () => {
|
|
104
|
+
expect(capitalize('hello')).toBe('Hello');
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe('slugify', () => {
|
|
109
|
+
it('converts to lowercase and replaces spaces', () => {
|
|
110
|
+
expect(slugify('Hello World')).toBe('hello-world');
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Create a skill definition at `workspace/skills/coding-standards/SKILL.md`. This tells the agent how to write and test code:
|
|
117
|
+
|
|
118
|
+
```markdown
|
|
119
|
+
---
|
|
120
|
+
name: coding-standards
|
|
121
|
+
description: Project coding standards and testing guidelines
|
|
122
|
+
---
|
|
123
|
+
|
|
124
|
+
# Coding Standards
|
|
125
|
+
|
|
126
|
+
## Code Quality
|
|
127
|
+
- Functions under 50 lines
|
|
128
|
+
- Use descriptive variable names
|
|
129
|
+
- Always add TypeScript types
|
|
130
|
+
|
|
131
|
+
## Testing
|
|
132
|
+
- Test all exported functions
|
|
133
|
+
- Use AAA pattern: Arrange, Act, Assert
|
|
134
|
+
- Cover happy paths and edge cases
|
|
135
|
+
|
|
136
|
+
## Before Committing
|
|
137
|
+
1. Write implementation
|
|
138
|
+
2. Write comprehensive tests
|
|
139
|
+
3. Run tests: `npm test`
|
|
140
|
+
4. All tests must pass
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Create a reference file at `workspace/skills/coding-standards/references/testing-guide.md` with detailed testing patterns:
|
|
144
|
+
|
|
145
|
+
````markdown
|
|
146
|
+
# Testing Guide
|
|
147
|
+
|
|
148
|
+
## AAA Pattern
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
it('descriptive test name', () => {
|
|
152
|
+
// Arrange: Set up test data
|
|
153
|
+
const input = 'test';
|
|
154
|
+
|
|
155
|
+
// Act: Execute the function
|
|
156
|
+
const result = doSomething(input);
|
|
157
|
+
|
|
158
|
+
// Assert: Verify the result
|
|
159
|
+
expect(result).toBe('expected');
|
|
160
|
+
});
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## What to Test
|
|
164
|
+
|
|
165
|
+
- Happy paths (normal inputs)
|
|
166
|
+
- Edge cases (empty, null, boundary values)
|
|
167
|
+
- Error cases (invalid inputs, exceptions)
|
|
168
|
+
````
|
|
169
|
+
|
|
170
|
+
## Create the dev assistant
|
|
171
|
+
|
|
172
|
+
With the workspace set up, it's time to create the development assistant agent. This agent will have instructions for adding new features using test-driven development (TDD).
|
|
173
|
+
|
|
174
|
+
Create a new file `src/mastra/agents/dev-assistant.ts` and define the agent:
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
import { Agent } from '@mastra/core/agent';
|
|
178
|
+
|
|
179
|
+
export const devAssistant = new Agent({
|
|
180
|
+
id: 'dev-assistant',
|
|
181
|
+
name: 'Dev Assistant',
|
|
182
|
+
instructions: `You are a development assistant.
|
|
183
|
+
|
|
184
|
+
When adding features:
|
|
185
|
+
1. Activate 'coding-standards' skill
|
|
186
|
+
2. Search workspace for similar code examples
|
|
187
|
+
3. Write the implementation following standards
|
|
188
|
+
4. Write comprehensive tests. Leave existing tests in place, only add your new tests
|
|
189
|
+
5. Execute the command \`npx vitest run\` to validate that all tests pass
|
|
190
|
+
6. Update documentation if needed
|
|
191
|
+
|
|
192
|
+
For every new feature: Write code → Write tests → Run tests → Update docs
|
|
193
|
+
|
|
194
|
+
Always explain your reasoning and steps.`,
|
|
195
|
+
model: 'openai/gpt-4o',
|
|
196
|
+
});
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
Define the agent by importing it inside `src/mastra/index.ts` and registering it with the `Mastra` instance:
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
import { Mastra } from '@mastra/core';
|
|
203
|
+
import { resolve } from 'node:path';
|
|
204
|
+
import { Workspace, LocalFilesystem, LocalSandbox } from '@mastra/core/workspace';
|
|
205
|
+
import { devAssistant } from './agents/dev-assistant';
|
|
206
|
+
|
|
207
|
+
const workspace = new Workspace({
|
|
208
|
+
filesystem: new LocalFilesystem({ basePath: resolve(import.meta.dirname, '../../workspace') }),
|
|
209
|
+
sandbox: new LocalSandbox({ workingDirectory: resolve(import.meta.dirname, '../../workspace') }),
|
|
210
|
+
skills: ['/skills'],
|
|
211
|
+
bm25: true,
|
|
212
|
+
autoIndexPaths: ['/docs', '/src'],
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
export const mastra = new Mastra({
|
|
216
|
+
workspace,
|
|
217
|
+
agents: { devAssistant },
|
|
218
|
+
});
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## Test the assistant
|
|
222
|
+
|
|
223
|
+
Start Mastra Studio and interact with the agent to see it in action.
|
|
224
|
+
|
|
225
|
+
**npm**:
|
|
226
|
+
|
|
227
|
+
```bash
|
|
228
|
+
npm run dev
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
**pnpm**:
|
|
232
|
+
|
|
233
|
+
```bash
|
|
234
|
+
pnpm run dev
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
**Yarn**:
|
|
238
|
+
|
|
239
|
+
```bash
|
|
240
|
+
yarn dev
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
**Bun**:
|
|
244
|
+
|
|
245
|
+
```bash
|
|
246
|
+
bun run dev
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
Open [localhost:4111](http://localhost:4111) and navigate to the dev assistant.
|
|
250
|
+
|
|
251
|
+
Try asking the agent to add a new function using TDD:
|
|
252
|
+
|
|
253
|
+
```text
|
|
254
|
+
Add a 'truncate' function to string-helpers.ts that shortens strings to a max length. Add '...' if truncated.
|
|
255
|
+
|
|
256
|
+
Follow TDD: write tests first, then implementation.
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
Since agent responses are non-deterministic, the exact output will vary. However, you should see the agent follow a process similar to this:
|
|
260
|
+
|
|
261
|
+
1. Activate the coding-standards skill
|
|
262
|
+
|
|
263
|
+
2. Search the workspace for similar code patterns
|
|
264
|
+
|
|
265
|
+
3. Write tests first, for example:
|
|
266
|
+
|
|
267
|
+
```typescript
|
|
268
|
+
describe('truncate', () => {
|
|
269
|
+
it('truncates long strings', () => {
|
|
270
|
+
expect(truncate('Hello World', 5)).toBe('He...');
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it('keeps short strings unchanged', () => {
|
|
274
|
+
expect(truncate('Hi', 10)).toBe('Hi');
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it('handles edge cases', () => {
|
|
278
|
+
expect(truncate('', 5)).toBe('');
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
4. Write the implementation, for example:
|
|
284
|
+
|
|
285
|
+
```typescript
|
|
286
|
+
export function truncate(str: string, maxLength: number): string {
|
|
287
|
+
if (!str || maxLength < 0) return str;
|
|
288
|
+
if (str.length <= maxLength) return str;
|
|
289
|
+
if (maxLength === 0) return '...';
|
|
290
|
+
return str.slice(0, maxLength - 3) + '...';
|
|
291
|
+
}
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
5. Run tests and verify they pass
|
|
295
|
+
|
|
296
|
+
## Next steps
|
|
297
|
+
|
|
298
|
+
You can extend this assistant to:
|
|
299
|
+
|
|
300
|
+
- Add more skills for different languages or frameworks
|
|
301
|
+
- Create specialized agents for backend, frontend, or DevOps
|
|
302
|
+
- Integrate with GitHub for automated PR reviews
|
|
303
|
+
- Build CI/CD automation
|
|
304
|
+
- Add multi-agent workflows
|