Skip to content

Commit 6181e43

Browse files
Add request to marshalled data
1 parent ea7355e commit 6181e43

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

cmd/docker-mcp/internal/interceptors/interceptors.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ func (i *Interceptor) ToMiddleware() mcp.Middleware {
117117
response, err := next(ctx, method, req)
118118

119119
if i.When == "after" {
120-
message, err := json.Marshal(response)
120+
message, err := json.Marshal(
121+
map[string]any{"request": req,
122+
"response": response})
121123
if err != nil {
122124
return nil, fmt.Errorf("marshalling response: %w", err)
123125
}

docs/interceptors.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Interceptors (beta)
2+
3+
Interceptors are extension points. They can mediate tool calls for any active MCP running in the gateway. This feature should be considered beta, and subject to change as we learn from users what they need to manage their MCP workloads.
4+
5+
## **After** Tool Calls
6+
7+
A simple method to experiment with interceptors is to register a script to run after a tool call has completed but before the response is sent back to the client. Start the gateway with the `--interceptor` argument and provide a local script.
8+
9+
```bash
10+
docker mcp gateway run --interceptor "after:exec:$HOME/script.sh"
11+
```
12+
13+
This would not make a good production configuration as it relies on an external script but this is a useful way to try out an interceptor on real MCP traffic. The interceptor script will receive a json payload on stdin, and _must_ return the possibly edited response payload on stdout. Here's a simple that just echos the response.
14+
15+
```bash
16+
#!/bin/bash
17+
18+
# Read JSON from stdin into a variable
19+
json_input=$(cat)
20+
21+
# Extract the response property and serialize it back to JSON
22+
echo "$json_input" | jq -r '.response | tostring'
23+
```
24+
25+
The incoming request will have both `request` and `response` properties.
26+
27+
```
28+
{
29+
"request": {
30+
"Session": {},
31+
"Params": {
32+
"_meta": {
33+
"progressToken": 1
34+
},
35+
"name": "get_me",
36+
"arguments": {}
37+
},
38+
"Extra": null
39+
},
40+
"response": {
41+
"content": [
42+
{
43+
"type": "text",
44+
"text": "..."
45+
}
46+
]
47+
}
48+
}
49+
50+
```
51+
52+
The interceptor must return a valid `ToolCall` response. Interceptors can easily blow your entire MCP workload. They can edit the response and are thus very powerful.
53+
54+
## **before** Tool Calls
55+

0 commit comments

Comments
 (0)