MCPcopy
hub / github.com/grpc-ecosystem/grpc-gateway

github.com/grpc-ecosystem/grpc-gateway @v2.29.0 sqlite

repository ↗ · DeepWiki ↗ · release v2.29.0 ↗
6,173 symbols 13,897 edges 289 files 1,250 documented · 20%
README

gRPC-Gateway

gRPC to JSON proxy generator following the gRPC HTTP spec

About

The gRPC-Gateway is a plugin of the Google protocol buffers compiler protoc. It reads protobuf service definitions and generates a reverse-proxy server which translates a RESTful HTTP API into gRPC. This server is generated according to the google.api.http annotations in your service definitions.

This helps you provide your APIs in both gRPC and RESTful style at the same time.

Docs

You can read our docs at:

  • https://grpc-ecosystem.github.io/grpc-gateway/

Testimonials

We use the gRPC-Gateway to serve millions of API requests per day, and have been since 2018 and through all of that, we have never had any issues with it.

- William Mill, Ad Hoc

Background

gRPC is great -- it generates API clients and server stubs in many programming languages, it is fast, easy-to-use, bandwidth-efficient and its design is combat-proven by Google. However, you might still want to provide a traditional RESTful JSON API as well. Reasons can range from maintaining backward-compatibility, supporting languages or clients that are not well supported by gRPC, to simply maintaining the aesthetics and tooling involved with a RESTful JSON architecture.

This project aims to provide that HTTP+JSON interface to your gRPC service. A small amount of configuration in your service to attach HTTP semantics is all that's needed to generate a reverse-proxy with this library.

Installation

Compile from source

The following instructions assume you are using Go Modules for dependency management. Use a tool dependency to track the versions of the following executable packages:

// +build tools

package tools

import (
    _ "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway"
    _ "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2"
    _ "google.golang.org/grpc/cmd/protoc-gen-go-grpc"
    _ "google.golang.org/protobuf/cmd/protoc-gen-go"
)

Run go mod tidy to resolve the versions. Install by running

go install \
    github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway \
    github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2 \
    google.golang.org/protobuf/cmd/protoc-gen-go \
    google.golang.org/grpc/cmd/protoc-gen-go-grpc

This will place four binaries in your $GOBIN;

  • protoc-gen-grpc-gateway
  • protoc-gen-openapiv2
  • protoc-gen-go
  • protoc-gen-go-grpc

Make sure that your $GOBIN is in your $PATH.

Using the tool Directive in Go 1.24

Starting from Go 1.24, the tool directive in go.mod provides a structured way to track and manage executable dependencies. This replaces the previous workaround of using a separate tools.go file with blank imports.

Tracking Tools in go.mod

Instead of manually importing tool dependencies in a Go source file, you can now use the tool directive in go.mod to declare the tools your project depends on. For example:

module tools

go 1.24

tool (
    github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway
    github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2
    google.golang.org/grpc/cmd/protoc-gen-go-grpc
    google.golang.org/protobuf/cmd/protoc-gen-go
)

Managing Tool Dependencies

To add tools to your module, use the -tool flag with go get:

go get -tool github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway
go get -tool github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2
go get -tool google.golang.org/protobuf/cmd/protoc-gen-go
go get -tool google.golang.org/grpc/cmd/protoc-gen-go-grpc

This automatically updates go.mod, adding the tools under the tool directive along with require statements to ensure version tracking.

Install Tools

Once the tool dependencies are properly recorded in the go.mod file, simply execute the following command in the root directory of your project:

go install tool

This will place four binaries in your $GOBIN;

  • protoc-gen-grpc-gateway
  • protoc-gen-openapiv2
  • protoc-gen-go
  • protoc-gen-go-grpc

Make sure that your $GOBIN is in your $PATH.

Download the binaries

You may alternatively download the binaries from the GitHub releases page. We generate SLSA3 signatures using the OpenSSF's slsa-framework/slsa-github-generator during the release process. To verify a release binary:

  1. Install the verification tool from slsa-framework/slsa-verifier#installation.
  2. Download the provenance file attestation.intoto.jsonl from the GitHub releases page.
  3. Run the verifier:
slsa-verifier -artifact-path <the-binary> -provenance attestation.intoto.jsonl -source github.com/grpc-ecosystem/grpc-gateway -tag <the-tag>

Alternatively, see the section on remotely managed plugin versions below.

Usage

1.Define your gRPC service using protocol buffers

your_service.proto:

 syntax = "proto3";
 package your.service.v1;
 option go_package = "github.com/yourorg/yourprotos/gen/go/your/service/v1";

 message StringMessage {
   string value = 1;
 }

 service YourService {
   rpc Echo(StringMessage) returns (StringMessage) {}
 }

2. Generate gRPC stubs

This step generates the gRPC stubs that you can use to implement the service and consume from clients:

Here's an example buf.gen.yaml you can use to generate the stubs with buf:

version: v2
plugins:
  - local: protoc-gen-go
    out: gen/go
    opt:
      - paths=source_relative
  - local: protoc-gen-go-grpc
    out: gen/go
    opt:
      - paths=source_relative

With this file in place, you can generate your files using buf generate.

For a complete example of using buf generate to generate protobuf stubs, see the boilerplate repo. For more information on generating the stubs with buf, see the official documentation.

If you are using protoc to generate stubs, here's an example of what a command might look like:

protoc -I . \
    --go_out ./gen/go/ --go_opt paths=source_relative \
    --go-grpc_out ./gen/go/ --go-grpc_opt paths=source_relative \
    your/service/v1/your_service.proto

3. Implement your service in gRPC as usual.

4. Generate reverse-proxy using protoc-gen-grpc-gateway

At this point, you have 3 options:

  • no further modifications, use the default mapping to HTTP semantics (method, path, etc.)
  • this will work on any .proto file, but will not allow setting HTTP paths, request parameters or similar
  • additional .proto modifications to use a custom mapping
  • relies on parameters in the .proto file to set custom HTTP mappings
  • no .proto modifications, but use an external configuration file
  • relies on an external configuration file to set custom HTTP mappings
  • mostly useful when the source proto file isn't under your control

1. Using the default mapping

This requires no additional modification to the .proto file but does require enabling a specific option when executing the plugin. The generate_unbound_methods should be enabled.

Here's what a buf.gen.yaml file might look like with this option enabled:

version: v2
plugins:
  - local: protoc-gen-go
    out: gen/go
    opt:
      - paths=source_relative
  - local: protoc-gen-go-grpc
    out: gen/go
    opt:
      - paths=source_relative
  - local: protoc-gen-grpc-gateway
    out: gen/go
    opt:
      - paths=source_relative
      - generate_unbound_methods=true

With protoc (just the grpc-gateway stubs):

protoc -I . --grpc-gateway_out ./gen/go \
    --grpc-gateway_opt paths=source_relative \
    --grpc-gateway_opt generate_unbound_methods=true \
    your/service/v1/your_service.proto

2. With custom annotations

Add a google.api.http annotation to your .proto file

your_service.proto:

 syntax = "proto3";
 package your.service.v1;
 option go_package = "github.com/yourorg/yourprotos/gen/go/your/service/v1";
+
+import "google/api/annotations.proto";
+
 message StringMessage {
   string value = 1;
 }

 service YourService {
-  rpc Echo(StringMessage) returns (StringMessage) {}
+  rpc Echo(StringMessage) returns (StringMessage) {
+    option (google.api.http) = {
+      post: "/v1/example/echo"
+      body: "*"
+    };
+  }
 }

You will need to provide the required third party protobuf files to the protobuf compiler. If you are using buf, this dependency can be added to the deps array in your buf.yaml under the name buf.build/googleapis/googleapis:

yaml version: v2 name: buf.build/yourorg/myprotos deps: - buf.build/googleapis/googleapis

Always run buf dep update after adding a dependency to your buf.yaml.

See a_bit_of_everything.proto for examples of more annotations you can add to customize gateway behavior and generated OpenAPI output.

Here's what a buf.gen.yaml file might look like:

version: v2
plugins:
  - local: protoc-gen-go
    out: gen/go
    opt:
      - paths=source_relative
  - local: protoc-gen-go-grpc
    out: gen/go
    opt:
      - paths=source_relative
  - local: protoc-gen-grpc-gateway
    out: gen/go
    opt:
      - paths=source_relative

If you are using protoc to generate stubs, you need to ensure the required dependencies are available to the compiler at compile time. These can be found by manually cloning and copying the relevant files from the googleapis repository, and providing them to protoc when running. The files you will need are:

google/api/annotations.proto
google/api/field_behavior.proto
google/api/http.proto
google/api/httpbody.proto

Here's what a protoc execution might look like:

protoc -I . --grpc-gateway_out ./gen/go \
    --grpc-gateway_opt paths=source_relative \
    your/service/v1/your_service.proto

3. External configuration

If you do not want to (or cannot) modify the proto file for use with gRPC-Gateway you can alternatively use an external gRPC Service Configuration file. Check our documentation for more information. This is best combined with the standalone=true option to generate a file that can live in its own package, separate from the files generated by the source protobuf file.

Here's what a buf.gen.yaml file might look like with this option enabled:

version: v2
plugins:
  - local: protoc-gen-go
    out: gen/go
    opt:
      - paths=source_relative
  - local: protoc-gen-go-grpc
    out: gen/go
    opt:
      - paths=source_relative
  - local: protoc-gen-grpc-gateway
    out: gen/go
    opt:
      - paths=source_relative
      - grpc_api_configuration=path/to/config.yaml
      - standalone=true

With protoc (just the grpc-gateway stubs):

protoc -I . --grpc-gateway_out ./gen/go \
    --grpc-gateway_opt paths=source_relative \
    --grpc-gateway_opt grpc_api_configuration=path/to/config.yaml \
    --grpc-gateway_opt standalone=true \
    your/service/v1/your_service.proto

5. Write an entrypoint for the HTTP reverse-proxy server

```go package main

import ( "context" "flag" "net/http"

"github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/grpclog"

gw "github.com/yourorg/yourrepo/proto/gen/go/your/service/v1/your_service" // Update )

var ( // command-line options: // gRPC server endpoint grpcServerEndpoint = flag.String("grpc-server-endpoint", "loca

Extension points exported contracts — how you extend this code

Marshaler (Interface)
Marshaler defines a conversion between byte sequence and gRPC payloads / fields. [4 implementers]
runtime/marshaler.go
Generator (Interface)
Generator is an abstraction of code generators. [2 implementers]
internal/generator/generator.go
Compiler (Interface)
Compiler compiles utilities representation of path templates into marshallable operations. They can be unmarshalled by r [1 …
internal/httprule/compile.go
QueryParameterParser (Interface)
QueryParameterParser defines interface for all query parameter parsers [1 implementers]
runtime/query.go
HandlerFunc (FuncType)
A HandlerFunc handles a specific pair of path pattern and HTTP method.
runtime/mux.go
ErrorHandlerFunc (FuncType)
ErrorHandlerFunc is the signature used to configure error handling.
runtime/errors.go
ContentEncoder (Interface)
(no doc) [1 implementers]
protoc-gen-openapiv2/internal/genopenapi/format.go
AnnotateContextOption (FuncType)
(no doc)
runtime/context.go

Core symbols most depended-on inside this repo

HTTPError
called by 612
runtime/errors.go
parameterToString
called by 491
examples/internal/clients/abe/client.go
MarshalerForRequest
called by 329
runtime/marshaler_registry.go
Handle
called by 323
runtime/mux.go
Header
called by 315
runtime/context.go
NewServerMetadataContext
called by 298
runtime/context.go
WithHTTPPathPattern
called by 297
runtime/context.go
GetForwardResponseOptions
called by 291
runtime/mux.go

Shape

Method 3,894
Function 1,427
Struct 628
Interface 115
TypeAlias 98
FuncType 11

Languages

Go100%
TypeScript1%

Modules by API surface

examples/internal/proto/examplepb/opaque.pb.go1,031 symbols
protoc-gen-openapiv2/options/openapiv2_protoopaque.pb.go437 symbols
protoc-gen-openapiv2/options/openapiv2.pb.go437 symbols
examples/internal/proto/examplepb/a_bit_of_everything.pb.go236 symbols
examples/internal/proto/examplepb/a_bit_of_everything_grpc.pb.go211 symbols
runtime/internal/examplepb/example.pb.go164 symbols
internal/descriptor/registry.go95 symbols
protoc-gen-openapiv2/internal/genopenapi/template_test.go93 symbols
examples/internal/proto/examplepb/visibility_rule_echo_service.pb.go93 symbols
protoc-gen-openapiv2/internal/genopenapi/template.go92 symbols
examples/internal/proto/examplepb/echo_service.pb.go90 symbols
examples/internal/proto/examplepb/a_bit_of_everything.pb.gw.go88 symbols

Dependencies from manifests, versioned

github.com/antihax/optionalv1.0.0 · 1×
github.com/kr/prettyv0.3.1 · 1×
github.com/rogpeppe/fastuuidv1.2.0 · 1×
go.yaml.in/yaml/v3v3.0.4 · 1×
golang.org/x/netv0.49.0 · 1×
golang.org/x/oauth2v0.36.0 · 1×
golang.org/x/sysv0.40.0 · 1×
golang.org/x/textv0.36.0 · 1×
google.golang.org/genproto/googleapis/apiv0.0.0-2026041400293 · 1×
google.golang.org/genproto/googleapis/rpcv0.0.0-2026041400293 · 1×
google.golang.org/grpcv1.80.0 · 1×

For agents

$ claude mcp add grpc-gateway \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact