Skip to content

Commit 371c0f9

Browse files
committed
simplify
1 parent c80b23b commit 371c0f9

File tree

263 files changed

+2693
-7943
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

263 files changed

+2693
-7943
lines changed

exercises/03.advanced-tools/03.problem.errors/README.mdx renamed to exercises/02.tools/03.problem.errors/README.mdx

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,6 @@ wrong—maybe a user tries to fetch a journal entry that doesn't exist, or there
55
a problem saving data. It's important that both users and LLMs get clear,
66
structured feedback when errors happen.
77

8-
For this step:
9-
10-
- Update your tools to return error responses in a way that's compatible with
11-
the MCP spec.
12-
- Make sure errors are surfaced in the response, with `isError: true` and a
13-
helpful message in the content.
14-
15-
This will make your journaling app more robust and user-friendly, and help LLMs
16-
handle problems gracefully.
17-
18-
<callout-info>
19-
ℹ️ In this exercise, you can simulate an error by trying to submit a tag with
20-
the word "error" in the name.
21-
</callout-info>
22-
238
Example error response:
249

2510
```json
@@ -38,4 +23,12 @@ Example error response:
3823
}
3924
```
4025

26+
Luckily for us, the MCP TypeScript SDK handles errors automatically. So all you
27+
need to do is throw an error from your tool, and the SDK will handle the rest!
28+
29+
For this step, we're going to say that tag names cannot include the word
30+
"error". If it does, throw an error with a descriptive message and then try it
31+
out in the inspector. You'll notice the `isError` field is set to `true` and the
32+
content is the helpful error message you provided.
33+
4134
- [📜 MCP Spec: Errors](https://modelcontextprotocol.io/specification/2025-03-26/server/tools#error-handling)

exercises/05.prompts/01.problem.prompts/package.json renamed to exercises/02.tools/03.problem.errors/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "exercises_05.prompts_01.problem.prompts",
2+
"name": "exercises_02.tools_03.problem.errors",
33
"private": true,
44
"type": "module",
55
"scripts": {

exercises/03.advanced-tools/01.problem.db/src/index.ts renamed to exercises/02.tools/03.problem.errors/src/index.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
22
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
33
import { z } from 'zod'
4-
// 🐨 bring in the database and initialize it.
5-
// 💰 here's the import:
6-
// import { DB } from './db/index.ts'
7-
8-
// 💰 Here's how you initialize the database:
9-
// const db = DB.getInstance('./db.sqlite')
104

115
const server = new McpServer(
126
{
@@ -17,25 +11,20 @@ const server = new McpServer(
1711
capabilities: {
1812
tools: {},
1913
},
20-
// 🐨 update the instructions to describe a journaling app
2114
instructions: 'This lets you solve math problems.',
2215
},
2316
)
2417

2518
server.tool(
26-
// 🐨 rename this tool to `create_tag`
2719
'add',
28-
// 🐨 update the description to describe a tool that creates a tag for entries
2920
'Add two numbers',
30-
// 🐨 update the arguments to take a name and description as arguments (💯 add a description to each argument)
3121
{
3222
firstNumber: z.number().describe('The first number to add'),
3323
secondNumber: z.number().describe('The second number to add'),
3424
},
35-
// 🐨 accept the tag as an argument
3625
async ({ firstNumber, secondNumber }) => {
37-
// 🐨 create the tag:
38-
// 💰 const createdTag = await db.createTag(tag)
26+
// 🐨 throw an error if the second number is negative
27+
// 💯 as a bonus, you can use the invariant function from @epic-web/invariant
3928
return {
4029
content: [
4130
{
File renamed without changes.

exercises/06.sampling/01.problem.simple/package.json renamed to exercises/02.tools/03.solution.errors/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "exercises_06.sampling_01.problem.simple",
2+
"name": "exercises_02.tools_03.solution.errors",
33
"private": true,
44
"type": "module",
55
"scripts": {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { invariant } from '@epic-web/invariant'
2+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
3+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
4+
import { z } from 'zod'
5+
6+
const server = new McpServer(
7+
{
8+
name: 'EpicMe',
9+
version: '1.0.0',
10+
},
11+
{
12+
capabilities: {
13+
tools: {},
14+
},
15+
instructions: 'This lets you solve math problems.',
16+
},
17+
)
18+
19+
server.tool(
20+
'add',
21+
'Add two numbers',
22+
{
23+
firstNumber: z.number().describe('The first number to add'),
24+
secondNumber: z.number().describe('The second number to add'),
25+
},
26+
async ({ firstNumber, secondNumber }) => {
27+
invariant(secondNumber >= 0, 'Second number cannot be negative')
28+
return {
29+
content: [
30+
{
31+
type: 'text',
32+
text: `The sum of ${firstNumber} and ${secondNumber} is ${firstNumber + secondNumber}.`,
33+
},
34+
],
35+
}
36+
},
37+
)
38+
39+
async function main() {
40+
const transport = new StdioServerTransport()
41+
await server.connect(transport)
42+
console.error('EpicMe MCP Server running on stdio')
43+
}
44+
45+
main().catch((error) => {
46+
console.error('Fatal error in main():', error)
47+
process.exit(1)
48+
})

0 commit comments

Comments
 (0)