$npx -y skills add Yourdaylight/stock_datasource --skill mcp-api-key-authThis skill should be used when the user needs to set up, manage, or troubleshoot MCP API Key authentication and tool usage tracking for this stock data service. Use it when configuring external MCP clients (Claude Code, Cursor) to connect to the data service.
| 1 | ## Purpose |
| 2 | |
| 3 | Enable external MCP clients (Claude Code, Cursor, etc.) to authenticate with the stock data service using independent API keys, and track per-tool usage (table name, record count) for monitoring. |
| 4 | |
| 5 | ## When to Use |
| 6 | |
| 7 | - User wants to create or manage MCP API keys |
| 8 | - User wants to configure an external MCP client to connect to this service |
| 9 | - User wants to view MCP tool usage statistics (which tools called, which tables queried, how many records) |
| 10 | - User needs to troubleshoot MCP authentication errors (401, invalid key, expired key) |
| 11 | |
| 12 | ## Architecture |
| 13 | |
| 14 | - **API Key management** is on the HTTP server (port 8000), protected by JWT login |
| 15 | - **MCP protocol** is on the MCP server (port 8001), protected by API key in request headers |
| 16 | - **Usage tracking** records are stored in ClickHouse `mcp_tool_usage_log` table |
| 17 | |
| 18 | ## Workflow |
| 19 | |
| 20 | ### 1) Create an API Key |
| 21 | |
| 22 | First login to get a JWT token, then create an API key: |
| 23 | |
| 24 | ```bash |
| 25 | # Login |
| 26 | JWT=$(curl -s -X POST http://<host>:8000/api/auth/login \ |
| 27 | -H "Content-Type: application/json" \ |
| 28 | -d '{"email":"your@email.com","password":"yourpassword"}' \ |
| 29 | | python3 -c "import sys,json; print(json.load(sys.stdin)['access_token'])") |
| 30 | |
| 31 | # Create API key |
| 32 | curl -s -X POST http://<host>:8000/api/mcp-keys/create \ |
| 33 | -H "Authorization: Bearer $JWT" \ |
| 34 | -H "Content-Type: application/json" \ |
| 35 | -d '{"key_name": "my-cursor-key", "expires_days": 90}' |
| 36 | ``` |
| 37 | |
| 38 | The response contains the full API key (e.g., `sk-a1b2c3d4...`). **Save it immediately** — it is only shown once. |
| 39 | |
| 40 | ### 2) Configure MCP Client |
| 41 | |
| 42 | #### Claude Code / Claude Desktop |
| 43 | |
| 44 | Add to your MCP configuration (`claude_desktop_config.json` or project `.mcp.json`): |
| 45 | |
| 46 | ```json |
| 47 | { |
| 48 | "mcpServers": { |
| 49 | "stock-data": { |
| 50 | "url": "http://<host>:8001/messages", |
| 51 | "transport": "streamable-http", |
| 52 | "headers": { |
| 53 | "Authorization": "Bearer sk-your-api-key-here" |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | ``` |
| 59 | |
| 60 | #### Cursor |
| 61 | |
| 62 | In Cursor settings, add an MCP server with: |
| 63 | - **URL**: `http://<host>:8001/messages` |
| 64 | - **Header**: `Authorization: Bearer sk-your-api-key-here` |
| 65 | |
| 66 | ### 3) Verify Connectivity |
| 67 | |
| 68 | ```bash |
| 69 | # Should return tool list (not 401) |
| 70 | curl -s -X POST http://<host>:8001/messages \ |
| 71 | -H "Content-Type: application/json" \ |
| 72 | -H "Authorization: Bearer sk-your-api-key-here" \ |
| 73 | -d '{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}' |
| 74 | ``` |
| 75 | |
| 76 | ### 4) View Usage Statistics |
| 77 | |
| 78 | ```bash |
| 79 | # Usage history (paginated) |
| 80 | curl -s "http://<host>:8000/api/mcp-usage/history?page=1&page_size=20" \ |
| 81 | -H "Authorization: Bearer $JWT" |
| 82 | |
| 83 | # Aggregated stats (last 30 days) |
| 84 | curl -s "http://<host>:8000/api/mcp-usage/stats?days=30" \ |
| 85 | -H "Authorization: Bearer $JWT" |
| 86 | ``` |
| 87 | |
| 88 | ### 5) Manage API Keys |
| 89 | |
| 90 | ```bash |
| 91 | # List all keys |
| 92 | curl -s http://<host>:8000/api/mcp-keys/list \ |
| 93 | -H "Authorization: Bearer $JWT" |
| 94 | |
| 95 | # Revoke a key |
| 96 | curl -s -X POST http://<host>:8000/api/mcp-keys/revoke \ |
| 97 | -H "Authorization: Bearer $JWT" \ |
| 98 | -H "Content-Type: application/json" \ |
| 99 | -d '{"key_id": "<key-id>"}' |
| 100 | ``` |
| 101 | |
| 102 | ## Key Endpoints |
| 103 | |
| 104 | | Endpoint | Method | Auth | Description | |
| 105 | |----------|--------|------|-------------| |
| 106 | | `/api/mcp-keys/create` | POST | JWT | Create new API key | |
| 107 | | `/api/mcp-keys/list` | GET | JWT | List user's API keys | |
| 108 | | `/api/mcp-keys/revoke` | POST | JWT | Revoke an API key | |
| 109 | | `/api/mcp-usage/history` | GET | JWT | Paginated usage history | |
| 110 | | `/api/mcp-usage/stats` | GET | JWT | Aggregated usage stats | |
| 111 | | `/messages` (MCP) | POST | API Key | MCP protocol endpoint | |
| 112 | |
| 113 | ## Troubleshooting |
| 114 | |
| 115 | - **401 "API key required"**: Missing `Authorization` header on MCP calls |
| 116 | - **401 "Invalid or expired API key"**: Key was revoked or expired — create a new one |
| 117 | - **`initialize` works but `tools/list` fails**: `initialize` does not require auth; `tools/list` and `tools/call` do |
| 118 | - **Usage not showing up**: Usage logging is fire-and-forget; check ClickHouse `mcp_tool_usage_log` table directly |
| 119 | |
| 120 | ## Verification Checklist |
| 121 | |
| 122 | - [ ] API key created and full key returned (shown only once) |
| 123 | - [ ] `tools/list` returns 401 without API key |
| 124 | - [ ] `tools/list` returns tool list with valid API key |
| 125 | - [ ] `tools/call` records usage in `mcp_tool_usage_log` table |
| 126 | - [ ] Revoked key returns 401 on subsequent calls |
| 127 | - [ ] Usage history API returns correct data |
| 128 | - [ ] Usage stats show daily call counts and top tools |