envoy-guide
Module 6 · Real-world Recipes·22 min·intermediate

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:

Shell
protoc \
  --include_imports \
  --include_source_info \
  --proto_path=. \
  --proto_path=./third_party/googleapis \
  --descriptor_set_out=mcp.pb \
  proto/mcp/v1/service.proto
--include_imports is not optional

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:

proto
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 requestRPC call
GET /v1/tools/abc123ToolService.GetTool with id = "abc123"
POST /v1/tools with JSON bodyToolService.CreateTool with that body as the tool field
GET /v1/tools/abc/logsToolService.StreamLogs with tool_id = "abc" → NDJSON

A few rules to internalize:

  • Path variables in curly-brace syntax (e.g. :id style) extract into named fields on the request message.
  • body: "tool" means "the JSON body deserializes into the tool field." 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: true to get NDJSON instead.

Wire it into Envoy

YAML
          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:

YAML
  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 }}}
Always set convert_grpc_status

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

Hands-on · One curl, one descriptor, one transcoded request

For the impatient version, use the bookstore sample Envoy ships:

Shell
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:

Shell
# 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 annotationWhat clients sendWhat the backend receives
get: "/v1/tools/:id"GET /v1/tools/abcGetTool with id = "abc"
post: "/v1/tools" + body: "tool"POST /v1/tools + JSON bodyCreateTool with body wrapped in tool field
post: "/v1/tools" + body: "*"POST /v1/tools + JSON bodyCreateTool with body as the whole request
get: "/v1/tools/:id/logs" streamingGET /v1/tools/abc/logsStreamLogs with tool_id = "abc" → NDJSON
Hot-reloading descriptors

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.

Real-world example · An MCP / API platform team (your use case)
Problem
The backend is an MCP server speaking gRPC. The clients are a mix: browsers, partner integrations, internal scripts, and a developer CLI — all of them want REST/JSON. Hand-rolling a JSON gateway service would be a parallel codebase to maintain forever.
Envoy solution
One Envoy with grpc_json_transcoder. The .proto file is the single source of truth; HTTP shape lives in google.api.http annotations next to the RPC definitions. Adding a new endpoint = annotating one new RPC + regenerating the descriptor. The MCP server stays gRPC-only, and every flavor of client gets first-class JSON access.

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.

Quick check

When generating the proto descriptor for the grpc_json_transcoder filter, you MUST pass…

Practical task
Finish the quick check and tick off the task to unlock.
Quick checkTasks 0/5