455 lines
6.3 KiB
Markdown
455 lines
6.3 KiB
Markdown
|
# Model Context Protocol 测试用例
|
||
|
|
||
|
本文档整理了从 Model Context Protocol 规范中收集的测试用例,主要来源于 https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/ 和 https://spec.modelcontextprotocol.io/specification/2024-11-05/server/ 。
|
||
|
|
||
|
## 基本协议测试用例
|
||
|
|
||
|
### 消息格式测试用例
|
||
|
|
||
|
#### 请求消息
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"id": "string | number",
|
||
|
"method": "string",
|
||
|
"params": {
|
||
|
"key": "value"
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 响应消息
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"id": "string | number",
|
||
|
"result": {
|
||
|
"key": "value"
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 通知消息
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"method": "string",
|
||
|
"params": {
|
||
|
"key": "value"
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### 生命周期测试用例
|
||
|
|
||
|
#### 初始化请求
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"id": 1,
|
||
|
"method": "initialize",
|
||
|
"params": {
|
||
|
"protocolVersion": "2024-11-05",
|
||
|
"capabilities": {
|
||
|
"roots": {
|
||
|
"listChanged": true
|
||
|
},
|
||
|
"sampling": {}
|
||
|
},
|
||
|
"clientInfo": {
|
||
|
"name": "ExampleClient",
|
||
|
"version": "1.0.0"
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 初始化响应
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"id": 1,
|
||
|
"result": {
|
||
|
"protocolVersion": "2024-11-05",
|
||
|
"capabilities": {
|
||
|
"logging": {},
|
||
|
"prompts": {
|
||
|
"listChanged": true
|
||
|
},
|
||
|
"resources": {
|
||
|
"subscribe": true,
|
||
|
"listChanged": true
|
||
|
},
|
||
|
"tools": {
|
||
|
"listChanged": true
|
||
|
}
|
||
|
},
|
||
|
"serverInfo": {
|
||
|
"name": "ExampleServer",
|
||
|
"version": "1.0.0"
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 初始化完成通知
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"method": "notifications/initialized"
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 初始化错误
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"id": 1,
|
||
|
"error": {
|
||
|
"code": -32602,
|
||
|
"message": "Unsupported protocol version",
|
||
|
"data": {
|
||
|
"supported": ["2024-11-05"],
|
||
|
"requested": "1.0.0"
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### 工具功能测试用例
|
||
|
|
||
|
#### Ping 请求
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"id": "123",
|
||
|
"method": "ping"
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### Ping 响应
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"id": "123",
|
||
|
"result": {}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### Cancellation 通知
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"method": "notifications/cancelled",
|
||
|
"params": {
|
||
|
"requestId": "123",
|
||
|
"reason": "User requested cancellation"
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### Progress 请求
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"id": 1,
|
||
|
"method": "some_method",
|
||
|
"params": {
|
||
|
"_meta": {
|
||
|
"progressToken": "abc123"
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### Progress 通知
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"method": "notifications/progress",
|
||
|
"params": {
|
||
|
"progressToken": "abc123",
|
||
|
"progress": 50,
|
||
|
"total": 100
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
## 服务器功能测试用例
|
||
|
|
||
|
### 工具功能测试用例
|
||
|
|
||
|
#### 列出工具请求
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"id": 1,
|
||
|
"method": "tools/list",
|
||
|
"params": {
|
||
|
"cursor": "optional-cursor-value"
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 列出工具响应
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"id": 1,
|
||
|
"result": {
|
||
|
"tools": [
|
||
|
{
|
||
|
"name": "get_weather",
|
||
|
"description": "Get current weather information for a location",
|
||
|
"inputSchema": {
|
||
|
"type": "object",
|
||
|
"properties": {
|
||
|
"location": {
|
||
|
"type": "string",
|
||
|
"description": "City name or zip code"
|
||
|
}
|
||
|
},
|
||
|
"required": ["location"]
|
||
|
}
|
||
|
}
|
||
|
],
|
||
|
"nextCursor": "next-page-cursor"
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 调用工具请求
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"id": 2,
|
||
|
"method": "tools/call",
|
||
|
"params": {
|
||
|
"name": "get_weather",
|
||
|
"arguments": {
|
||
|
"location": "New York"
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 调用工具响应
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"id": 2,
|
||
|
"result": {
|
||
|
"content": [
|
||
|
{
|
||
|
"type": "text",
|
||
|
"text": "Current weather in New York:\nTemperature: 72°F\nConditions: Partly cloudy"
|
||
|
}
|
||
|
],
|
||
|
"isError": false
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 工具列表变更通知
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"method": "notifications/tools/list_changed"
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### 资源功能测试用例
|
||
|
|
||
|
#### 列出资源请求
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"id": 1,
|
||
|
"method": "resources/list",
|
||
|
"params": {
|
||
|
"cursor": "optional-cursor-value"
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 列出资源响应
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"id": 1,
|
||
|
"result": {
|
||
|
"resources": [
|
||
|
{
|
||
|
"uri": "file:///project/src/main.rs",
|
||
|
"name": "main.rs",
|
||
|
"description": "Primary application entry point",
|
||
|
"mimeType": "text/x-rust"
|
||
|
}
|
||
|
],
|
||
|
"nextCursor": "next-page-cursor"
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 读取资源请求
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"id": 2,
|
||
|
"method": "resources/read",
|
||
|
"params": {
|
||
|
"uri": "file:///project/src/main.rs"
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 读取资源响应
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"id": 2,
|
||
|
"result": {
|
||
|
"contents": [
|
||
|
{
|
||
|
"uri": "file:///project/src/main.rs",
|
||
|
"mimeType": "text/x-rust",
|
||
|
"text": "fn main() {\n println!(\"Hello world!\");\n}"
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 资源模板列表请求
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"id": 3,
|
||
|
"method": "resources/templates/list"
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 资源模板列表响应
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"id": 3,
|
||
|
"result": {
|
||
|
"resourceTemplates": [
|
||
|
{
|
||
|
"uriTemplate": "file:///{path}",
|
||
|
"name": "Project Files",
|
||
|
"description": "Access files in the project directory",
|
||
|
"mimeType": "application/octet-stream"
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 资源列表变更通知
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"method": "notifications/resources/list_changed"
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 订阅资源请求
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"id": 4,
|
||
|
"method": "resources/subscribe",
|
||
|
"params": {
|
||
|
"uri": "file:///project/src/main.rs"
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 资源更新通知
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"method": "notifications/resources/updated",
|
||
|
"params": {
|
||
|
"uri": "file:///project/src/main.rs"
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 资源错误响应
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"jsonrpc": "2.0",
|
||
|
"id": 5,
|
||
|
"error": {
|
||
|
"code": -32002,
|
||
|
"message": "Resource not found",
|
||
|
"data": {
|
||
|
"uri": "file:///nonexistent.txt"
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### 工具结果数据类型
|
||
|
|
||
|
#### 文本内容
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"type": "text",
|
||
|
"text": "Tool result text"
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 图像内容
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"type": "image",
|
||
|
"data": "base64-encoded-data",
|
||
|
"mimeType": "image/png"
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 嵌入资源
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"type": "resource",
|
||
|
"resource": {
|
||
|
"uri": "resource://example",
|
||
|
"mimeType": "text/plain",
|
||
|
"text": "Resource content"
|
||
|
}
|
||
|
}
|
||
|
```
|