JSON ↔ gRPC transcoding
Expose a gRPC backend as a REST/JSON API without writing a translation layer.
The killer feature
You have a gRPC backend. Your callers — browsers, mobile apps, partners,
curl, your own ops scripts — speak JSON over HTTP. The grpc_json_transcoder
filter sits in the middle and translates both directions, with no
hand-written shim service.
Think of it as grpc-gateway built into your proxy — but with rate
limiting, auth, retries, and observability already in the same hop.
The contract: a proto descriptor
The transcoder is configured with a binary proto descriptor — your
.proto files compiled into a single self-describing file. Build it once
with protoc:
protoc \
--include_imports \
--include_source_info \
--proto_path=. \
--proto_path=./third_party/googleapis \
--descriptor_set_out=mcp.pb \
proto/mcp/v1/service.proto
Forgetting --include_imports ships a descriptor without
transitive deps (like google/api/annotations.proto). Envoy
fails to load with a cryptic "Unable to parse proto descriptor" error.
This is the most common transcoding bug and it bites everyone once.
You'll also need the googleapis common protos
(google/api/annotations.proto, google/api/http.proto) — clone them from
github.com/googleapis/googleapis
and add the path to --proto_path.
Annotate your .proto with HTTP routes
The mapping from URLs to RPCs lives in your .proto, via google.api.http
annotations:
syntax = "proto3";
package mcp.v1;
import "google/api/annotations.proto";
service ToolService {
rpc GetTool(GetToolRequest) returns (Tool) {
option (google.api.http) = {
get: "/v1/tools/{id}"
};
}
rpc CreateTool(CreateToolRequest) returns (Tool) {
option (google.api.http) = {
post: "/v1/tools"
body: "tool"
};
}
rpc StreamLogs(StreamLogsRequest) returns (stream LogEvent) {
option (google.api.http) = {
get: "/v1/tools/{tool_id}/logs"
};
}
}
message GetToolRequest { string id = 1; }
message CreateToolRequest { Tool tool = 1; }
message Tool { string id = 1; string name = 2; string image = 3; }
message StreamLogsRequest { string tool_id = 1; }
message LogEvent { string ts = 1; string line = 2; }
How those annotations actually map:
| HTTP request | RPC call |
|---|---|
GET /v1/tools/abc123 | ToolService.GetTool with id = "abc123" |
POST /v1/tools with JSON body | ToolService.CreateTool with that body as the tool field |
GET /v1/tools/abc/logs | ToolService.StreamLogs with tool_id = "abc" → NDJSON |
A few rules to internalize:
- Path variables in curly-brace syntax (e.g.
:idstyle) extract into named fields on the request message. body: "tool"means "the JSON body deserializes into thetoolfield."body: "*"would mean "the entire JSON body is the request message."- Unmatched query params silently become message fields unless you
set
ignore_unknown_query_parameters: true. - Streaming RPCs return a streaming JSON array by default; set
stream_newline_delimited: trueto get NDJSON instead.
Wire it into Envoy
http_filters:
- name: envoy.filters.http.grpc_json_transcoder
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.grpc_json_transcoder.v3.GrpcJsonTranscoder
proto_descriptor: /etc/envoy/mcp.pb
services: ["mcp.v1.ToolService"]
print_options:
add_whitespace: true
always_print_primitive_fields: true
preserve_proto_field_names: false # camelCase JSON
convert_grpc_status: true # map grpc-status → HTTP status
request_validation_options:
reject_unknown_method: true
reject_unknown_query_parameters: true
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
And the cluster — same http2_protocol_options requirement as Lesson 17,
because the upstream is gRPC:
clusters:
- name: mcp_cluster
type: STRICT_DNS
connect_timeout: 1s
typed_extension_protocol_options:
envoy.extensions.upstreams.http.v3.HttpProtocolOptions:
"@type": type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions
explicit_http_config: { http2_protocol_options: {} }
load_assignment:
cluster_name: mcp_cluster
endpoints:
- lb_endpoints:
- endpoint: { address: { socket_address: { address: mcp-server, port_value: 50051 }}}
With convert_grpc_status: true, a NOT_FOUND
(grpc-status 5) becomes HTTP 404 with a JSON body containing
code and message fields. Without it, your client
gets HTTP 200 + a confusing trailer it probably won't read. Turn it on.
Hands-on: REST in, gRPC out
For the impatient version, use the bookstore sample Envoy ships:
git clone --depth 1 https://github.com/envoyproxy/envoy
cd envoy/examples/grpc-bridge
docker compose up
This brings up an Envoy with the transcoder pre-configured and a Python gRPC bookstore behind it. Then:
# JSON in, JSON out — but the backend is gRPC.
curl -s localhost:51051/shelves
curl -s -X POST localhost:51051/shelves \
-H 'content-type: application/json' \
--data-raw '"theme":"Cooking"'
(In real usage you'd send a full JSON object as the body — escaping curlies inside a code block inside a JSX element is awkward, so the snippet above uses a flat form. The point is one curl, gRPC backend.)
Read envoy.yaml — the grpc_json_transcoder
block is the whole magic.
When you've seen it work, kill it (docker compose down) and
adapt the same pattern to your own .proto: write annotations, build the
descriptor, point the filter at it.
Mapping rules in one table
| .proto annotation | What clients send | What the backend receives |
|---|---|---|
get: "/v1/tools/:id" | GET /v1/tools/abc | GetTool with id = "abc" |
post: "/v1/tools" + body: "tool" | POST /v1/tools + JSON body | CreateTool with body wrapped in tool field |
post: "/v1/tools" + body: "*" | POST /v1/tools + JSON body | CreateTool with body as the whole request |
get: "/v1/tools/:id/logs" streaming | GET /v1/tools/abc/logs | StreamLogs with tool_id = "abc" → NDJSON |
The descriptor is read at config load. Adding new RPCs requires regenerating
mcp.pb and reloading Envoy's config. Plan your release flow
accordingly — or use SDS-style dynamic loading via xDS in a real mesh.
What's next
You can now expose any gRPC backend as JSON. The next two lessons protect that endpoint — auth (Lesson 19) and rate limiting (Lesson 20) — before you stack everything together into the full MCP gateway in Lesson 21.
When generating the proto descriptor for the grpc_json_transcoder filter, you MUST pass…