---
title: "Intercept Requests and Responses"
description: "Claude Code ส่งอะไรมาหา server ของเราบ้าง ลองดูข้อความจริงด้วย curl, MCP Inspector และ mitmproxy"
canonical_url: "https://iamthana.com/courses/mcp-server/inspect-traffic"
---

> Index of every page on this site: [llms.txt](https://iamthana.com/llms.txt)

บทที่แล้ว Claude Code เรียก `list_menu` ให้เราได้แล้ว แต่เรายังไม่เห็นเลยว่ามันส่งอะไรมาหา server บ้าง ถ้าวันไหน tool ไม่ทำงาน เราจะไม่รู้ว่าพังที่ฝั่งไหน

บทนี้เราจะเปิดดูข้อความ JSON-RPC ที่วิ่งระหว่าง client กับ server ด้วย curl (คำสั่งส่ง HTTP request จาก terminal), MCP Inspector และ mitmproxy ครับ

จบบทนี้เราจะส่ง request หา server เองได้โดยไม่ต้องมี AI และเห็นทุก request ที่ Claude Code ส่ง

ทั้งหมดใช้ server ตัวเดิมจากบทที่แล้ว แค่เปลี่ยน transport จาก stdio เป็น HTTP เพราะ HTTP มีเครื่องมือดักดูให้ใช้เยอะกว่า

## 1. Run the Server over HTTP

สร้างไฟล์ `http_server.py` ไว้ในโฟลเดอร์ `cafe-mcp`

```python [http_server.py]
from cafe_server import mcp

mcp.run(transport="streamable-http", host="127.0.0.1", port=8765)
```

ไฟล์นี้ import `mcp` ตัวเดิมจาก `cafe_server.py` มารันแบบ HTTP แทน โค้ด tool ไม่ต้องแก้

เปิด terminal แยกไว้หนึ่งหน้าต่าง `cd` เข้า `cafe-mcp` แล้วรัน

```bash
uv run python http_server.py
```

```text [$ uv run python http_server.py]
INFO:     Uvicorn running on http://127.0.0.1:8765 (Press CTRL+C to quit)
```

เห็นบรรทัดนี้แปลว่า server รอรับ request ที่ `http://127.0.0.1:8765/mcp` แล้ว เปิดทิ้งไว้ทั้งบทเลยนะ

## 2. Test with curl

เปิด terminal อีกหน้าต่าง แล้วตั้งตัวแปรไว้ก่อน คำสั่งจะได้สั้นลง

```bash
URL=http://127.0.0.1:8765/mcp
META='"_meta":{"io.modelcontextprotocol/protocolVersion":"2026-07-28","io.modelcontextprotocol/clientCapabilities":{}}'
```

`META` คือ `_meta` ที่ทุก request ต้องแนบตามที่เล่าไว้ในบทแรก มี 2 key ที่ขาดไม่ได้ คือเวอร์ชันของ protocol กับสิ่งที่ client ทำได้

### List the Tools

```bash
curl -s $URL \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json, text/event-stream' \
  -H 'MCP-Protocol-Version: 2026-07-28' \
  -H 'Mcp-Method: tools/list' \
  -d "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/list\",\"params\":{$META}}" \
  | python3 -m json.tool
```

ผลที่ได้ (ตัดเนื้อใน `inputSchema` กับ `outputSchema` ออกให้สั้นลง)

```json [$ curl -s $URL -H 'Mcp-Method: tools/list']
{
    "jsonrpc": "2.0",
    "id": 1,
    "result": {
        "cacheScope": "private",
        "resultType": "complete",
        "tools": [
            {
                "description": "List the cafe menu with prices in baht. Pass max_price_thb to show only drinks at or below that price.",
                "inputSchema": { },
                "name": "list_menu",
                "outputSchema": { }
            }
        ],
        "ttlMs": 0,
        "_meta": {
            "io.modelcontextprotocol/serverInfo": {
                "name": "cafe",
                "version": ""
            }
        }
    }
}
```

นี่คือสิ่งที่ Claude Code ได้เห็นตอนเริ่มคุยกับ server ของเราครับ ชื่อ tool, docstring ที่กลายเป็น `description` และ `inputSchema` กับ `outputSchema`

### Call a Tool

```bash
curl -s $URL \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json, text/event-stream' \
  -H 'MCP-Protocol-Version: 2026-07-28' \
  -H 'Mcp-Method: tools/call' \
  -H 'Mcp-Name: list_menu' \
  -d "{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"tools/call\",\"params\":{\"name\":\"list_menu\",\"arguments\":{\"max_price_thb\":50},$META}}" \
  | python3 -m json.tool
```

```json [$ curl -s $URL -H 'Mcp-Method: tools/call']
{
    "jsonrpc": "2.0",
    "id": 2,
    "result": {
        "content": [
            {
                "text": "{\n  \"name\": \"thai tea\",\n  \"price_thb\": 45,\n  \"available\": false\n}",
                "type": "text"
            },
            {
                "text": "{\n  \"name\": \"americano\",\n  \"price_thb\": 50,\n  \"available\": true\n}",
                "type": "text"
            }
        ],
        "isError": false,
        "resultType": "complete",
        "structuredContent": {
            "result": [
                {
                    "name": "thai tea",
                    "price_thb": 45,
                    "available": false
                },
                {
                    "name": "americano",
                    "price_thb": 50,
                    "available": true
                }
            ]
        },
        "_meta": {
            "io.modelcontextprotocol/serverInfo": {
                "name": "cafe",
                "version": ""
            }
        }
    }
}
```

### What These Headers Do

สังเกตว่าเราต้องส่งชื่อ method ซ้ำสองที่ ทั้งใน body (`"method":"tools/call"`) และใน header (`Mcp-Method: tools/call`) ส่วนชื่อ tool ก็ซ้ำใน `Mcp-Name`

spec `2026-07-28` บังคับแบบนี้เพื่อให้ตัวกลางที่อยู่หน้า server (เช่น load balancer ที่กระจาย request ไปหลายเครื่อง) อ่าน header แล้วรู้เลยว่า request นี้จะเรียกอะไร ไม่ต้องเปิด body มา parse ก่อน เช่น จำกัดจำนวนครั้งที่เรียก `tools/call` ได้จาก header อย่างเดียว

- `Mcp-Method` ต้องมีทุก request
- `Mcp-Name` ต้องมีเมื่อเป็น `tools/call`, `resources/read` และ `prompts/get`

ลองส่งให้ header กับ body ไม่ตรงกันดู (header บอก `tools/list` แต่ body เป็น `tools/call`)

```bash
curl -s -w '\nHTTP %{http_code}\n' $URL \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json, text/event-stream' \
  -H 'MCP-Protocol-Version: 2026-07-28' \
  -H 'Mcp-Method: tools/list' \
  -H 'Mcp-Name: list_menu' \
  -d "{\"jsonrpc\":\"2.0\",\"id\":3,\"method\":\"tools/call\",\"params\":{\"name\":\"list_menu\",\"arguments\":{},$META}}"
```

```text [$ curl -s -w '\nHTTP %{http_code}\n' $URL][$ curl -s -w '\nHTTP %\n' $URL]
{"jsonrpc":"2.0","id":3,"error":{"code":-32020,"message":"mcp-method header does not match the request body's method"}}
HTTP 400
```

server ไม่ยอมทำงานให้เลย ตอบ error `-32020` กลับมาแทน

## 3. Try MCP Inspector

[MCP Inspector](https://github.com/modelcontextprotocol/inspector) เป็นเครื่องมือทางการสำหรับลองเรียก server โดยไม่ต้องผ่าน AI รันด้วย `npx` (รันโปรแกรมจาก npm โดยไม่ติดตั้งลงเครื่อง) เลยต้องมี Node.js ในเครื่อง (เวอร์ชัน 2.16.0 ต้องการ Node 22.23.4+, 24.22.1+ หรือ 26.12.1+)

### CLI

```bash
npx -y @modelcontextprotocol/inspector@2.16.0 --cli http://127.0.0.1:8765/mcp \
  --transport http \
  --method tools/call --tool-name list_menu --tool-arg max_price_thb=55
```

```json [$ npx -y @modelcontextprotocol/inspector@2.16.0 --cli http://127.0.0.1:8765/mcp]
{
  "content": [
    {
      "type": "text",
      "text": "{\n  \"name\": \"thai tea\",\n  \"price_thb\": 45,\n  \"available\": false\n}"
    },
    {
      "type": "text",
      "text": "{\n  \"name\": \"americano\",\n  \"price_thb\": 50,\n  \"available\": true\n}"
    }
  ],
  "structuredContent": {
    "result": [
      {
        "name": "thai tea",
        "price_thb": 45,
        "available": false
      },
      {
        "name": "americano",
        "price_thb": 50,
        "available": true
      }
    ]
  },
  "isError": false
}
```

ผลออกมาเหมือน curl แต่ไม่มี `resultType` กับ `_meta` เพราะ Inspector เวอร์ชันนี้ยังคุยด้วย MCP เวอร์ชันเก่า (`2025-11-25`) เป็นค่าเริ่มต้น (`resultType` มีไว้ทำอะไร บทหน้าจะเล่า)

Python SDK ของเรารองรับทั้งสองเวอร์ชัน เลยตอบได้ทั้งคู่ ถ้าอยากให้ Inspector คุยด้วย `2026-07-28` เหมือน curl ให้เติม `--protocol-era modern`

### Web UI

```bash
npx -y @modelcontextprotocol/inspector@2.16.0 --web
```

ตอนเปิด Inspector จะพิมพ์ URL ที่มี `MCP_INSPECTOR_API_TOKEN` ต่อท้ายออกมาใน terminal ให้เปิดจากลิงก์นั้น จากนั้น

<steps level="4">

#### Add the Server

กด **Add Servers** แล้วเลือก **Add manually** ตั้ง **Server ID** เป็น `cafe` (ช่องนี้ต้องกรอก) transport เป็น `streamable-http` และ URL เป็น `http://127.0.0.1:8765/mcp`

ในรายการจะมี server ตัวอย่างที่ Inspector ใส่มาให้อีก 3 ตัว ไม่ต้องสนใจ

#### Connect

server ที่เพิ่งเพิ่มจะขึ้นว่า Disconnected กดสวิตช์ข้าง `cafe` ให้เป็น Connected

#### Call a Tool

ไปที่แท็บ **Tools** เลือก `list_menu` ใส่ **Max Price Thb** เป็น `55` แล้วกด **Execute Tool** จากนั้นกดแท็บ **Protocol** ที่แผงขวา (ค่าเริ่มต้นจะเปิดแท็บ Network ไว้)

</steps>

![MCP Inspector แท็บ Tools เรียก list_menu ด้วย max_price_thb 55 ตรงกลางเป็นผลลัพธ์และ Structured Output ฝั่งขวาเป็นแผง Protocol ที่มีป้าย LEGACY และ JSON-RPC ของ tools/call](https://iamthana.com/images/courses/mcp-server/inspector-tools-list-menu.webp)

แผง **Protocol** ฝั่งขวาโชว์ request กับ response ดิบของทุกครั้งที่กดเรียก และมีป้าย `LEGACY` บอกว่ากำลังคุยแบบเก่าอยู่

## 4. Intercept Claude Code with mitmproxy

curl กับ Inspector คือเราเป็นคนส่งเอง ถ้าอยากรู้ว่า Claude Code ส่งอะไรมาบ้าง ต้องมีตัวกลางคั่นไว้ครับ

[mitmproxy](https://mitmproxy.org/) เป็นตัวกลางแบบนั้น (`uvx` ในคำสั่งข้างล่างรันโปรแกรมจาก package โดยไม่ติดตั้งลงโปรเจกต์)

เราจะตั้งให้มันเป็น reverse proxy คือ Claude Code ต่อเข้า mitmproxy ที่ port 8766 แล้ว mitmproxy ส่งต่อไปให้ server จริงที่ 8765 พร้อมเก็บทุก request ไว้ให้ดูบนหน้าเว็บ

`http_server.py` ต้องเปิดค้างไว้ แล้วเปิด terminal เพิ่มอีก 2 หน้าต่างสำหรับ mitmweb กับ `claude` (Inspector แบบ Web UI ปิดด้วย Ctrl+C ได้เลย)

### Start mitmweb

```bash
uvx --from mitmproxy==12.3.1 mitmweb \
  --mode reverse:http://127.0.0.1:8765 \
  --listen-host 127.0.0.1 --listen-port 8766 \
  --web-port 8081 \
  --set web_password=cafe-demo
```

เปิด `http://127.0.0.1:8081` แล้วใส่รหัส `cafe-demo` (ถ้าไม่ตั้ง `web_password` mitmweb จะพิมพ์ลิงก์ที่มี token ออกมาใน terminal แทน เปิดหน้าเว็บตรง ๆ จะเจอหน้า login ให้กรอก token นั้น)

ตรงนี้ใช้ http ธรรมดาทั้งเส้น ไม่ต้องติดตั้ง certificate ของ mitmproxy ลงเครื่อง (ต้องใช้ก็ต่อเมื่อจะดัก https)

### Route Claude Code Through the Proxy

ถ้ายังมี server `cafe` แบบ stdio จากบทที่แล้วอยู่ Claude Code จะเลือกเรียก tool จากตัวนั้นแทน request ก็จะไม่วิ่งผ่าน mitmproxy เลยถอดออกก่อน แล้วเพิ่มตัวที่ต่อผ่าน proxy เข้าไป (รันในโฟลเดอร์ `cafe-mcp`)

```bash
claude mcp remove --scope project cafe
claude mcp add --scope project --transport http cafe-http http://127.0.0.1:8766/mcp
```

เปิด `claude` กดอนุมัติ `cafe-http` เหมือนบทที่แล้ว แล้วถามคำถามเดิม

```text
ร้านมีเมนูอะไรบ้างที่ราคาไม่เกิน 55 บาท และสั่งได้
```

กลับไปดูที่ mitmweb จะเห็น request วิ่งเข้ามาหลายอันเลย

![mitmweb แสดง flow 8 อัน ทุกอันเป็น POST ไปที่ /mcp เจ็ดอันสถานะ 200 หนึ่งอันเป็น 202 และสองอันมีไอคอนเตือนสีแดงเพราะ stream ถูกปิดก่อนส่งจบ](https://iamthana.com/images/courses/mcp-server/mitmweb-flows.webp)

ทุกอันเป็น `POST /mcp` เหมือนกันหมด ต้องกดเข้าไปดู header `Mcp-Method` ทีละอันถึงจะรู้ว่าเป็นอะไร (mitmweb โชว์ชื่อ header เป็นตัวเล็กหมด เช่น `mcp-method` ซึ่งความหมายเหมือนกัน)

ลำดับจาก Claude Code 2.1.326 เป็นแบบนี้

<table>
<thead>
  <tr>
    <th>
      #
    </th>
    
    <th>
      Mcp-Method
    </th>
    
    <th>
      Status
    </th>
    
    <th>
      Purpose
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      1
    </td>
    
    <td>
      <code>
        server/discover
      </code>
    </td>
    
    <td>
      200
    </td>
    
    <td>
      ถามว่า server เป็นใคร รองรับอะไรบ้าง
    </td>
  </tr>
  
  <tr>
    <td>
      2
    </td>
    
    <td>
      <code>
        subscriptions/listen
      </code>
    </td>
    
    <td>
      200
    </td>
    
    <td>
      เปิด stream ค้างไว้รอฟังว่ามีอะไรเปลี่ยน
    </td>
  </tr>
  
  <tr>
    <td>
      3
    </td>
    
    <td>
      <code>
        notifications/cancelled
      </code>
    </td>
    
    <td>
      202
    </td>
    
    <td>
      แจ้งยกเลิก stream ข้อ 2 เป็น notification (ข้อความที่ไม่รอคำตอบ) server เลยตอบแค่ 202 ไม่มี body
    </td>
  </tr>
  
  <tr>
    <td>
      4
    </td>
    
    <td>
      <code>
        prompts/list
      </code>
    </td>
    
    <td>
      200
    </td>
    
    <td>
      ขอรายการ prompt
    </td>
  </tr>
  
  <tr>
    <td>
      5
    </td>
    
    <td>
      <code>
        resources/list
      </code>
    </td>
    
    <td>
      200
    </td>
    
    <td>
      ขอรายการ resource
    </td>
  </tr>
  
  <tr>
    <td>
      6
    </td>
    
    <td>
      <code>
        tools/list
      </code>
    </td>
    
    <td>
      200
    </td>
    
    <td>
      ขอรายการ tool
    </td>
  </tr>
  
  <tr>
    <td>
      7
    </td>
    
    <td>
      <code>
        subscriptions/listen
      </code>
    </td>
    
    <td>
      200
    </td>
    
    <td>
      เปิด stream รอฟังอีกรอบ
    </td>
  </tr>
  
  <tr>
    <td>
      8
    </td>
    
    <td>
      <code>
        tools/call
      </code>
    </td>
    
    <td>
      200
    </td>
    
    <td>
      เรียก <code>
        list_menu
      </code>
      
       ด้วย <code>
        max_price_thb: 55
      </code>
    </td>
  </tr>
</tbody>
</table>

สองอันที่มีไอคอนเตือนสีแดงคือ `subscriptions/listen` ไม่ใช่ error ของ MCP เป็น stream ที่ Claude Code เปิดค้างไว้แล้วปิดเอง mitmproxy เลยบันทึกว่าการเชื่อมต่อถูกตัด

กดเข้าไปที่อันสุดท้าย จะเห็น header ครบตามที่เราทดสอบด้วย curl

![รายละเอียด request ของ tools/call ใน mitmweb มี header mcp-method tools/call, mcp-name list_menu, mcp-protocol-version 2026-07-28 และ body JSON ที่มี arguments max_price_thb 55](https://iamthana.com/images/courses/mcp-server/mitmweb-tools-call-request.webp)

![response ของ tools/call ใน mitmweb สถานะ 200 body มี content, structuredContent และ resultType complete](https://iamthana.com/images/courses/mcp-server/mitmweb-tools-call-response.webp)

เทียบกับ Inspector ที่ขึ้นป้าย `LEGACY` จะเห็นว่า Claude Code คุยด้วย `2026-07-28` ทั้งหมด ทุก request แนบเวอร์ชันกับ `_meta` มาเองครบ server เลยไม่ต้องจำอะไรจาก request ก่อนหน้า

ทดลองเสร็จแล้วถอด `cafe-http` ออก แล้วใส่ `cafe` แบบ stdio กลับเข้าไปเหมือนเดิม บทต่อ ๆ ไปจะใช้ตัวนี้ (รันในโฟลเดอร์ `cafe-mcp` เพราะคำสั่งใช้ `$(pwd)`)

```bash
claude mcp remove --scope project cafe-http
claude mcp add --scope project cafe -- uv run --directory "$(pwd)" python cafe_server.py
```

จากนั้นหยุด `http_server.py`, mitmweb และ Inspector ด้วย Ctrl+C ได้ทั้งหมด

## Pitfalls

- server ตัวนี้ไม่มี authentication ใครต่อถึง port 8765 ได้ก็เรียก tool ได้หมด เราเลย bind ไว้ที่ `127.0.0.1` ซึ่งรับ request จากเครื่องตัวเองเท่านั้น ห้ามเปลี่ยนเป็น `0.0.0.0` (รับจากทุกเครื่องในเครือข่าย) จนกว่าจะใส่ auth
- mitmproxy เห็นทุกอย่างที่วิ่งผ่าน ถ้าวันหลังเอาไปคั่น server ที่มี token ใน header token ก็จะไปโผล่ในหน้า mitmweb ด้วย ใส่ `--listen-host 127.0.0.1` และตั้ง `web_password` ทุกครั้ง
- token ใน URL ของ Inspector เป็นกุญแจเข้าหน้า Inspector อย่า copy ไปแปะในแชทหรือ issue
- pin เวอร์ชันทุกครั้งที่รันผ่าน `npx` หรือ `uvx` เพราะสองคำสั่งนี้โหลดโค้ดจาก registry มารันในเครื่องทันที ถ้าไม่ใส่เลขเวอร์ชัน วันไหนมีเวอร์ชันใหม่ (รวมถึงเวอร์ชันที่มีคนแอบใส่โค้ดอันตราย) ก็จะถูกโหลดมารันเลย

## Summary

curl เรียก server ได้เองถ้าแนบ header `MCP-Protocol-Version`, `Mcp-Method` (กับ `Mcp-Name`) และ `_meta` ครบ Inspector ลองเรียก tool โดยไม่ต้องมี AI ส่วน mitmproxy แสดงว่าคำถามเดียวใน Claude Code กลายเป็น 8 request

บทหน้าจะเทียบว่าแบบเก่าที่ Inspector ใช้ กับแบบใหม่ที่ Claude Code ใช้ ต่างกันตรงไหนบ้างครับ

## References

- [Streamable HTTP transport (spec 2026-07-28)](https://modelcontextprotocol.io/specification/2026-07-28/basic/transports/streamable-http)
- [MCP Inspector](https://github.com/modelcontextprotocol/inspector)
- [mitmproxy: reverse proxy mode](https://docs.mitmproxy.org/stable/concepts/modes/#reverse-proxy)

## Sitemap

See the full [sitemap](https://iamthana.com/sitemap.md) for all pages.
