|
| 1 | +import { app } from '@/index' |
| 2 | + |
| 3 | +describe('Test /mcp', () => { |
| 4 | + it('Should return initialize response', async () => { |
| 5 | + const res = await app.request('/mcp', { |
| 6 | + method: 'POST', |
| 7 | + body: JSON.stringify({ |
| 8 | + jsonrpc: '2.0', |
| 9 | + id: 1, |
| 10 | + method: 'initialize', |
| 11 | + params: { |
| 12 | + protocolVersion: '2024-11-05', |
| 13 | + clientInfo: { |
| 14 | + name: 'test-client', |
| 15 | + version: '0.0.0', |
| 16 | + }, |
| 17 | + capabilities: { tools: true }, |
| 18 | + }, |
| 19 | + }), |
| 20 | + headers: { |
| 21 | + 'Content-Type': 'application/json', |
| 22 | + Accept: 'application/json, text/event-stream', |
| 23 | + }, |
| 24 | + }) |
| 25 | + |
| 26 | + const messages = await parseSSEJSONResponse(res) |
| 27 | + const result = messages.find((m) => m.id === 1) |
| 28 | + |
| 29 | + expect(result.result.serverInfo.name).toBe('Ramen API MCP Server') |
| 30 | + expect(res.status).toBe(200) |
| 31 | + }) |
| 32 | + |
| 33 | + it('Should return a list of tools with correct properties', async () => { |
| 34 | + const res = await app.request('/mcp', { |
| 35 | + method: 'POST', |
| 36 | + body: JSON.stringify({ |
| 37 | + jsonrpc: '2.0', |
| 38 | + id: 2, |
| 39 | + method: 'tools/list', |
| 40 | + params: {}, |
| 41 | + }), |
| 42 | + headers: { |
| 43 | + 'Content-Type': 'application/json', |
| 44 | + Accept: 'application/json, text/event-stream', |
| 45 | + }, |
| 46 | + }) |
| 47 | + |
| 48 | + const messages = await parseSSEJSONResponse(res) |
| 49 | + const result = messages.find((m) => m.id === 2) |
| 50 | + |
| 51 | + expect(res.status).toBe(200) |
| 52 | + expect(result).toHaveProperty('result') |
| 53 | + expect(Array.isArray(result.result.tools)).toBe(true) |
| 54 | + expect(result.result.tools.length).toBeGreaterThan(0) |
| 55 | + |
| 56 | + const getShopsTool = result.result.tools.find( |
| 57 | + (tool) => tool.name === 'get_shops' |
| 58 | + ) |
| 59 | + expect(getShopsTool).toBeDefined() |
| 60 | + expect(getShopsTool).toHaveProperty('description', 'Get ramen shops') |
| 61 | + expect(getShopsTool).toHaveProperty('inputSchema') |
| 62 | + expect(getShopsTool.inputSchema).toHaveProperty('type', 'object') |
| 63 | + expect(getShopsTool.inputSchema.properties).toHaveProperty('perPage') |
| 64 | + expect(getShopsTool.inputSchema.properties.perPage).toHaveProperty( |
| 65 | + 'type', |
| 66 | + 'number' |
| 67 | + ) |
| 68 | + expect(getShopsTool.inputSchema.properties.perPage).toHaveProperty( |
| 69 | + 'minimum', |
| 70 | + 1 |
| 71 | + ) |
| 72 | + expect(getShopsTool.inputSchema.properties).toHaveProperty('page') |
| 73 | + expect(getShopsTool.inputSchema.properties.page).toHaveProperty( |
| 74 | + 'type', |
| 75 | + 'number' |
| 76 | + ) |
| 77 | + expect(getShopsTool.inputSchema.properties.page).toHaveProperty( |
| 78 | + 'minimum', |
| 79 | + 1 |
| 80 | + ) |
| 81 | + |
| 82 | + const getShopTool = result.result.tools.find( |
| 83 | + (tool) => tool.name === 'get_shop' |
| 84 | + ) |
| 85 | + expect(getShopTool).toBeDefined() |
| 86 | + expect(getShopTool).toHaveProperty('description', 'Get a shop information') |
| 87 | + expect(getShopTool).toHaveProperty('inputSchema') |
| 88 | + expect(getShopTool.inputSchema).toHaveProperty('type', 'object') |
| 89 | + expect(getShopTool.inputSchema.properties).toHaveProperty('shopId') |
| 90 | + expect(getShopTool.inputSchema.properties.shopId).toHaveProperty( |
| 91 | + 'type', |
| 92 | + 'string' |
| 93 | + ) |
| 94 | + }) |
| 95 | + |
| 96 | + it('Should execute get_shops tool and return a list of shops', async () => { |
| 97 | + const res = await app.request('/mcp', { |
| 98 | + method: 'POST', |
| 99 | + body: JSON.stringify({ |
| 100 | + jsonrpc: '2.0', |
| 101 | + id: 3, |
| 102 | + method: 'tools/call', |
| 103 | + params: { |
| 104 | + name: 'get_shops', |
| 105 | + arguments: { |
| 106 | + perPage: 5, |
| 107 | + page: 1, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }), |
| 111 | + headers: { |
| 112 | + 'Content-Type': 'application/json', |
| 113 | + Accept: 'application/json, text/event-stream', |
| 114 | + }, |
| 115 | + }) |
| 116 | + |
| 117 | + const messages = await parseSSEJSONResponse(res) |
| 118 | + const result = messages.find((m) => m.id === 3) |
| 119 | + |
| 120 | + expect(res.status).toBe(200) |
| 121 | + expect(result).toHaveProperty('result') |
| 122 | + expect(result.result).toHaveProperty('content') |
| 123 | + expect(Array.isArray(result.result.content)).toBe(true) |
| 124 | + |
| 125 | + const content = result.result.content[0] |
| 126 | + expect(content).toHaveProperty('type', 'text') |
| 127 | + expect(content).toHaveProperty('text') |
| 128 | + |
| 129 | + const shops = JSON.parse(content.text) |
| 130 | + expect(shops).toHaveProperty('shops') |
| 131 | + expect(Array.isArray(shops.shops)).toBe(true) |
| 132 | + expect(shops.shops.length).toBeGreaterThan(0) |
| 133 | + }) |
| 134 | + |
| 135 | + it('Should execute get_shop tool and return shop details', async () => { |
| 136 | + const res = await app.request('/mcp', { |
| 137 | + method: 'POST', |
| 138 | + body: JSON.stringify({ |
| 139 | + jsonrpc: '2.0', |
| 140 | + id: 4, |
| 141 | + method: 'tools/call', |
| 142 | + params: { |
| 143 | + name: 'get_shop', |
| 144 | + arguments: { |
| 145 | + shopId: 'yoshimuraya', |
| 146 | + }, |
| 147 | + }, |
| 148 | + }), |
| 149 | + headers: { |
| 150 | + 'Content-Type': 'application/json', |
| 151 | + Accept: 'application/json, text/event-stream', |
| 152 | + }, |
| 153 | + }) |
| 154 | + |
| 155 | + const messages = await parseSSEJSONResponse(res) |
| 156 | + const result = messages.find((m) => m.id === 4) |
| 157 | + |
| 158 | + expect(res.status).toBe(200) |
| 159 | + |
| 160 | + expect(result).toHaveProperty('result') |
| 161 | + expect(result.result).toHaveProperty('content') |
| 162 | + expect(Array.isArray(result.result.content)).toBe(true) |
| 163 | + |
| 164 | + const content = result.result.content[0] |
| 165 | + expect(content).toHaveProperty('type', 'text') |
| 166 | + expect(content).toHaveProperty('text') |
| 167 | + |
| 168 | + const shop = JSON.parse(content.text) |
| 169 | + expect(shop).toHaveProperty('id', 'yoshimuraya') |
| 170 | + expect(shop).toHaveProperty('name') // 店名が存在することを確認 |
| 171 | + expect(shop).toHaveProperty('photos') // 写真データが存在することを確認 |
| 172 | + expect(Array.isArray(shop.photos)).toBe(true) |
| 173 | + }) |
| 174 | +}) |
| 175 | + |
| 176 | +export async function parseSSEJSONResponse(res: Response) { |
| 177 | + const text = await res.text() |
| 178 | + const lines = text.split('\n') |
| 179 | + const dataLines = lines.filter((line) => line.startsWith('data: ')) |
| 180 | + const jsonStrings = dataLines.map((line) => line.slice(6)) |
| 181 | + return jsonStrings.map((json) => JSON.parse(json)) |
| 182 | +} |
0 commit comments