Skip to content

Commit f303c19

Browse files
update xmcp to v0.5.0 (#1221)
### Description xmcp now features prompts on its latest release, would be great to bump to v0.2 so it initializes with it by default ### Type of Change - [ ] New Example - [X] Example updates (Bug fixes, new features, etc.) - [ ] Other (changes to the codebase, but not to examples)
1 parent 0aad1c1 commit f303c19

File tree

10 files changed

+511
-4036
lines changed

10 files changed

+511
-4036
lines changed

framework-boilerplates/xmcp/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ node_modules/
33
.vercel
44
dist
55
.xmcp
6-
xmcp-env.d.ts
6+
xmcp-env.d.ts
7+
.env

framework-boilerplates/xmcp/README.md

Lines changed: 104 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,90 @@ This will start the MCP server with the selected transport method.
1818

1919
## Project Structure
2020

21-
This project uses the structured approach where tools are automatically discovered from the `src/tools` directory. Each tool is defined in its own file with the following structure:
21+
This project uses the structured approach where tools, prompts, and resources are automatically discovered from their respective directories:
22+
23+
- `src/tools` - Tool definitions
24+
- `src/prompts` - Prompt templates
25+
- `src/resources` - Resource handlers
26+
27+
### Tools
28+
29+
Each tool is defined in its own file with the following structure:
2230

2331
```typescript
24-
import { z } from 'zod'
25-
import { type InferSchema } from 'xmcp'
32+
import { z } from "zod";
33+
import { type InferSchema, type ToolMetadata } from "xmcp";
2634

27-
// Define the schema for tool parameters
2835
export const schema = {
29-
a: z.number().describe('First number to add'),
30-
b: z.number().describe('Second number to add'),
31-
}
36+
name: z.string().describe("The name of the user to greet"),
37+
};
3238

33-
// Define tool metadata
34-
export const metadata = {
35-
name: 'add',
36-
description: 'Add two numbers together',
39+
export const metadata: ToolMetadata = {
40+
name: "greet",
41+
description: "Greet the user",
3742
annotations: {
38-
title: 'Add Two Numbers',
43+
title: "Greet the user",
3944
readOnlyHint: true,
4045
destructiveHint: false,
4146
idempotentHint: true,
4247
},
48+
};
49+
50+
export default function greet({ name }: InferSchema<typeof schema>) {
51+
return `Hello, ${name}!`;
52+
}
53+
```
54+
55+
### Prompts
56+
57+
Prompts are template definitions for AI interactions:
58+
59+
```typescript
60+
import { z } from "zod";
61+
import { type InferSchema, type PromptMetadata } from "xmcp";
62+
63+
export const schema = {
64+
code: z.string().describe("The code to review"),
65+
};
66+
67+
export const metadata: PromptMetadata = {
68+
name: "review-code",
69+
title: "Review Code",
70+
description: "Review code for best practices and potential issues",
71+
role: "user",
72+
};
73+
74+
export default function reviewCode({ code }: InferSchema<typeof schema>) {
75+
return `Please review this code: ${code}`;
4376
}
77+
```
78+
79+
### Resources
80+
81+
Resources provide data or content with URI-based access:
82+
83+
```typescript
84+
import { z } from "zod";
85+
import { type ResourceMetadata, type InferSchema } from "xmcp";
4486

45-
// Tool implementation
46-
export default async function add({ a, b }: InferSchema<typeof schema>) {
47-
return {
48-
content: [{ type: 'text', text: String(a + b) }],
49-
}
87+
export const schema = {
88+
userId: z.string().describe("The ID of the user"),
89+
};
90+
91+
export const metadata: ResourceMetadata = {
92+
name: "user-profile",
93+
title: "User Profile",
94+
description: "User profile information",
95+
};
96+
97+
export default function handler({ userId }: InferSchema<typeof schema>) {
98+
return `Profile data for user ${userId}`;
5099
}
51100
```
52101

53-
## Adding New Tools
102+
## Adding New Components
103+
104+
### Adding New Tools
54105

55106
To add a new tool:
56107

@@ -59,6 +110,25 @@ To add a new tool:
59110
3. Export a `metadata` object with tool information
60111
4. Export a default function that implements the tool logic
61112

113+
### Adding New Prompts
114+
115+
To add a new prompt:
116+
117+
1. Create a new `.ts` file in the `src/prompts` directory
118+
2. Export a `schema` object defining the prompt parameters using Zod
119+
3. Export a `metadata` object with prompt information and role
120+
4. Export a default function that returns the prompt text
121+
122+
### Adding New Resources
123+
124+
To add a new resource:
125+
126+
1. Create a new `.ts` file in the `src/resources` directory
127+
2. Use folder structure to define the URI (e.g., `(users)/[userId]/profile.ts``users://{userId}/profile`)
128+
3. Export a `schema` object for dynamic parameters (optional for static resources)
129+
4. Export a `metadata` object with resource information
130+
5. Export a default function that returns the resource content
131+
62132
## Building for Production
63133

64134
To build your project for production:
@@ -80,17 +150,27 @@ You can run the server for the transport built with:
80150
- HTTP: `node dist/http.js`
81151
- STDIO: `node dist/stdio.js`
82152

83-
Alternatively, you can use the script which will automatically start the appropriate transport based on your project configuration:
153+
Given the selected transport method, you will have a custom start script added to the `package.json` file.
84154

85-
```
86-
npm run start
155+
For HTTP:
156+
157+
```bash
158+
npm run start-http
87159
# or
88-
yarn start
160+
yarn start-http
89161
# or
90-
pnpm start
162+
pnpm start-http
91163
```
92164

93-
The start script will automatically run either the HTTP or STDIO transport depending on which transport method was selected when you initialized the project.
165+
For STDIO:
166+
167+
```bash
168+
npm run start-stdio
169+
# or
170+
yarn start-stdio
171+
# or
172+
pnpm start-stdio
173+
```
94174

95175
## Learn More
96176

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "xmcp-demo",
33
"version": "0.1.0",
4-
"description": "An xmcp application with tool syntax",
4+
"description": "An xmcp demo server with tool syntax",
55
"engines": {
66
"node": "22.x"
77
},
@@ -11,10 +11,7 @@
1111
"start": "node dist/http.js"
1212
},
1313
"dependencies": {
14-
"xmcp": "^0.1.11",
14+
"xmcp": "^0.5.0",
1515
"zod": "3.24.4"
16-
},
17-
"devDependencies": {
18-
"swc-loader": "^0.2.6"
1916
}
2017
}

0 commit comments

Comments
 (0)