# E2E Spec: MCP JSON-RPC Protocol

## Overview
Test the MCP JSON-RPC 2.0 protocol infrastructure shared by server-scoped MCP routes.
Covers `desktop/internal/wire/mcp/protocol.go` route registration, auth middleware, RPC dispatch,
batch processing, and SSE streaming.

## Setup
Construct a Gin router with `mcp.Dynamic()` using a test MCPHandler.
Use `failpoint.Enable("agentteam-config-dir", ...)` to isolate token key storage.

## Test Cases

### Initialize Handshake
- POST with `{"jsonrpc":"2.0","method":"initialize","id":1}` returns protocol version "2024-11-05"
- Response includes `capabilities.tools.listChanged=true`
- Response includes `serverInfo.name="agentteam"`

### Ping
- POST with `{"jsonrpc":"2.0","method":"ping","id":2}` returns `{"result":{}}`

### Tools List
- POST with `{"jsonrpc":"2.0","method":"tools/list","id":3}` returns tool definitions
- Nil Properties in tool definitions are serialized as `{}` (not null)

### Tools Call
- POST with `{"jsonrpc":"2.0","method":"tools/call","params":{"name":"message_send","arguments":{...}},"id":4}`
- Routes to handler's CallTool method
- Missing params returns error code -32602
- Unknown tool returns error from handler

### Method Not Found
- POST with unknown method returns error code -32601

### Notification Handling
- Request without `id` field returns 202 Accepted with no body
- Batch of all notifications returns 202 Accepted

### Batch Processing
- POST with array of requests returns array of responses
- Mixed batch (requests + notifications) returns responses only for requests

### Auth Middleware (AccessControl)
- Missing agentId param returns 400
- Missing Authorization header returns 401
- Invalid token returns 401
- Token with wrong agent returns 403
- Valid agent token returns 200 and sets agentID in context

### SSE Stream (GET)
- GET with Accept: text/event-stream returns SSE stream with ": ready" prefix
- Keepalive sent every 25 seconds
- GET without SSE Accept header returns JSON info

### DELETE Disconnect
- DELETE returns 204 No Content

### Body Size Limit
- Request body exceeding 4MB returns 400

### Malformed JSON
- Invalid JSON body returns -32700 parse error
- Empty body returns 400

### Server Resolution
- Request for unknown serverId returns 404
- Request for known serverId routes to correct handler
