resource-design
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCreate xmcp Resource
创建xmcp资源
You are helping the user create a new xmcp resource. Follow this interactive workflow.
你正在协助用户创建新的xmcp资源,请遵循以下交互式工作流程。
Step 1: Gather Information
步骤1:收集信息
Before generating any code, ask the user these questions using AskUserQuestion:
-
Resource name (if not provided): What should the resource be named? (Use kebab-case)
-
Resource type: Ask which type of resource they need:
- Static - Fixed content that doesn't change (configuration, documentation)
- Dynamic - Content that depends on parameters (user profiles, reports)
- File-based - Content read from files (logs, data files)
-
URI pattern: How should the resource be accessed?
- Simple: or
config://appdocs://readme - With parameters:
users://[userId]/profile - Nested:
reports://[year]/[month]/summary
- Simple:
-
Content details:
- What is the content about?
-
Widget support (optional): Ask if they need OpenAI widget rendering.
在生成任何代码之前,使用AskUserQuestion向用户询问以下问题:
-
资源名称(若未提供):资源应命名为什么?(使用短横线分隔命名法kebab-case)
-
资源类型:询问用户需要哪种类型的资源:
- 静态资源 - 内容固定不变(配置文件、文档)
- 动态资源 - 内容依赖参数(用户配置文件、报告)
- 基于文件的资源 - 从文件读取内容(日志、数据文件)
-
URI模式:资源应如何被访问?
- 简单模式:或
config://appdocs://readme - 带参数模式:
users://[userId]/profile - 嵌套模式:
reports://[year]/[month]/summary
- 简单模式:
-
内容详情:
- 资源内容涉及什么主题?
-
组件支持(可选):询问用户是否需要OpenAI组件渲染支持。
Step 2: Generate the Resource
步骤2:生成资源
Create the resource file in following the routing conventions:
src/resources/按照路由约定在目录下创建资源文件:
src/resources/File Location & Routing
文件位置与路由
Resources use file-based routing similar to Next.js:
src/resources/
├── (config)/ # Route group (not in URI)
│ └── app.ts # → config://app
├── (users)/
│ └── [userId]/ # Dynamic segment
│ └── profile.ts # → users://[userId]/profile
└── docs/
└── readme.ts # → docs://readmeRouting conventions:
- - Route group, excluded from URI path
(folder) - - Dynamic parameter segment
[param] - Filename becomes the final URI segment
资源采用与Next.js类似的基于文件的路由机制:
src/resources/
├── (config)/ # 路由组(不会出现在URI中)
│ └── app.ts # → config://app
├── (users)/
│ └── [userId]/ # 动态分段
│ └── profile.ts # → users://[userId]/profile
└── docs/
└── readme.ts # → docs://readme路由约定:
- - 路由组,不会包含在URI路径中
(folder) - - 动态参数分段
[param] - 文件名会成为URI的最终分段
Resource Structure Reference
资源结构参考
Every xmcp resource has two main exports:
typescript
// 1. Metadata (optional) - Resource configuration
export const metadata: ResourceMetadata = { /* ... */ };
// 2. Handler (required) - Default export function
export default function handler(params?) { /* ... */ }
// 3. Schema (optional) - For dynamic resources with parameters
export const schema = { /* ... */ };每个xmcp资源都包含两个主要导出项:
typescript
// 1. 元数据(可选)- 资源配置
export const metadata: ResourceMetadata = { /* ... */ };
// 2. 处理器(必填)- 默认导出函数
export default function handler(params?) { /* ... */ }
// 3. Schema(可选)- 适用于带参数的动态资源
export const schema = { /* ... */ };Quick Reference
快速参考
Essential Imports
必要导入
typescript
import { type ResourceMetadata } from "xmcp";
// For dynamic resources
import { z } from "zod";
import { type InferSchema, type ResourceMetadata } from "xmcp";typescript
import { type ResourceMetadata } from "xmcp";
// 针对动态资源
import { z } from "zod";
import { type InferSchema, type ResourceMetadata } from "xmcp";Metadata Fields
元数字段
| Field | Type | Required | Description |
|---|---|---|---|
| string | No* | Unique resource identifier |
| string | No | Human-readable title |
| string | No | What this resource provides |
| string | No | Content type (default: text/plain) |
| number | No | Content size in bytes |
| object | No | Vendor extensions (OpenAI, etc.) |
*When metadata is omitted, xmcp uses the filename as the resource name.
| 字段 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| string | 否* | 唯一资源标识符 |
| string | 否 | 易读的标题 |
| string | 否 | 资源提供的内容说明 |
| string | 否 | 内容类型(默认值:text/plain) |
| number | 否 | 内容大小(字节) |
| object | 否 | 厂商扩展(如OpenAI等) |
*当省略元数据时,xmcp会将文件名作为资源名称。
Handler Return Types
处理器返回类型
| Return Type | Use Case |
|---|---|
| Simple text content |
| Explicit text content |
| Binary content |
| JSON content |
| 返回类型 | 使用场景 |
|---|---|
| 简单文本内容 |
| 显式文本内容 |
| 二进制内容 |
| JSON内容 |
Detailed Templates
详细模板
For complete code templates including:
- Static resource patterns
- Dynamic resource with parameters
- File-based resources
- Nested routing examples
- OpenAI metadata examples
Read:
references/patterns.md如需完整的代码模板,包括:
- 静态资源模式
- 带参数的动态资源
- 基于文件的资源
- 嵌套路由示例
- OpenAI元数据示例
阅读:
references/patterns.mdChecklist After Generation
生成后的检查清单
- File created in correct location
src/resources/ - If using metadata, ensure it has descriptive and
namedescription - Schema uses for all parameters (if dynamic)
.describe() - Handler returns appropriate content type
- File path matches intended URI pattern
Suggest running to verify the resource compiles correctly.
pnpm build- 文件已创建在正确的目录下
src/resources/ - 若使用元数据,确保其包含描述性的和
namedescription - 若为动态资源,Schema中所有参数都使用进行说明
.describe() - 处理器返回了合适的内容类型
- 文件路径与预期的URI模式匹配
建议运行来验证资源是否能正确编译。
pnpm build