agentleFS
dallaydallay/agents-skills/skills/grpc-api/SKILL.md

grpc-api

Skill2 starsChanged 4 months agoMITView on GitHub

Build high-performance gRPC services with Protocol Buffers, streaming patterns, and microservice communication in Node.js and polyglot environments. Use when the task involves `gRPC server or client`, `Protocol Buffers`, `protobuf service definition`, `gRPC streaming`, `microservice communication`, or `inter-service APIs`.

The file

We split this skill into the rules it gives an agent

12 rules. About 0 are common, 7 are distinctive, and 5 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈7

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 5

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Contract First: Always define your .proto file before writing any server or client code.

    line 28
  2. Distinctive:

    Streaming for Large Data: Use server streaming for large result sets and client streaming for bulk uploads.

    line 34
  3. Distinctive:

    Deadlines Over Timeouts: Always set deadlines on client calls.

    line 36
  4. Very specific:

    TLS in Production: Never use createInsecure() credentials outside of local development.

    line 38
  5. Distinctive:

    Keep Messages Flat: Avoid deeply nested message types.

    line 39
  6. Very specific:

    Use clear, descriptive message and service naming (CreateUserRequest, not Req1).

    line 336
  7. Distinctive:

    Add metadata for request tracing (request ID, correlation ID).

    line 338
  8. Distinctive:

    Use server streaming for large datasets instead of massive unary responses.

    line 340
  9. Very specific:

    Use TLS credentials in production (grpc.ServerCredentials.createSsl()).

    line 342
  10. Distinctive:

    Use gRPC directly from browsers — use gRPC-Web or a REST gateway instead.

    line 347
  11. Distinctive:

    Create deeply nested message types — keep messages flat and composable.

    line 349
  12. Very specific:

    Use createInsecure() credentials outside of local development.

    line 353

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

gRPC API Development

Build efficient gRPC services using Protocol Buffers for contract-first API design, with support for unary calls, server streaming, client streaming, and bidirectional streaming.

When to Use

  • Building microservices that require high-performance binary communication.
  • Defining strict service contracts with Protocol Buffers (.proto files).
  • Implementing real-time bidirectional or server-push streaming.
  • Creating internal service-to-service APIs in polyglot architectures.
  • Optimizing bandwidth in constrained or high-throughput environments.

Critical Patterns

  • Contract First: Always define your .proto file before writing any server or client code. The proto IS your API contract.
  • Use Proper Status Codes: Map domain errors to gRPC status codes (NOT_FOUND, ALREADY_EXISTS, INVALID_ARGUMENT, etc.). Never return raw exceptions.
  • Field Numbering is Forever: Once a proto field number is assigned and released, NEVER reuse it. Add new fields with new numbers; deprecate old ones.
  • Streaming for Large Data: Use server streaming for large result sets and client streaming for bulk uploads. Avoid sending massive unary payloads.
  • Deadlines Over Timeouts: Always set deadlines on client calls. A missing deadline can hang forever in production.
  • TLS in Production: Never use createInsecure() credentials outside of local development.
  • Keep Messages Flat: Avoid deeply nested message types. Flatten where possible for better wire efficiency and readability.

Proto Definition Patterns

Complete Service Definition

syntax = "proto3";

package user.service;

message User {
  string id = 1;
  string email = 2;
  string first_name = 3;
  string last_name = 4;
  string role = 5;
  int64 created_at = 6;
  int64 updated_at = 7;
}

message CreateUserRequest {
  string email = 1;
  string first_name = 2;
  string last_name = 3;
  string role = 4;
}

message UpdateUserRequest {
  string id = 1;
  string email = 2;
  string first_name = 3;
  string last_name = 4;
}

message GetUserRequest {
  string id = 1;
}

message ListUsersRequest {
  int32 page = 1;
  int32 limit = 2;
}

message ListUsersResponse {
  repeated User users = 1;
  int32 total = 2;
  int32 page = 3;
}

message DeleteUserRequest {
  string id = 1;
}

message Empty {}

// Four RPC patterns: unary, server streaming, client streaming, bidirectional
service UserService {
  rpc GetUser(GetUserRequest) returns (User);              // Unary
  rpc ListUsers(ListUsersRequest) returns (ListUsersResponse); // Unary
  rpc CreateUser(CreateUserRequest) returns (User);        // Unary
  rpc UpdateUser(UpdateUserRequest) returns (User);        // Unary
  rpc DeleteUser(DeleteUserRequest) returns (Empty);       // Unary
  rpc StreamUsers(Empty) returns (stream User);            // Server streaming
  rpc BulkCreateUsers(stream CreateUserRequest) returns (ListUsersResponse); // Client streaming
}

Event Streaming Service

message Event {
  string type = 1;
  string user_id = 2;
  string data = 3;
  int64 timestamp = 4;
}

service EventService {
  rpc Subscribe(Empty) returns (stream Event);   // Server streaming
  rpc PublishEvent(Event) returns (Empty);        // Unary
}

Node.js Server Implementation

Loading Proto and Implementing Handlers

const grpc = require("@grpc/grpc-js");
const protoLoader = require("@grpc/proto-loader");
const path = require("path");

const packageDef = protoLoader.loadSync(path.join(__dirname, "user.proto"), {
  keepCase: true,
  longs: String,
  enums: String,
  defaults: true,
  oneofs: true,
});

const userProto = grpc.loadPackageDefinition(packageDef).user.service;

const users = new Map();
let userIdCounter = 1;

const userServiceImpl = {
  // Unary RPC — single request, single response
  getUser: (call, callback) => {
    const user = users.get(call.request.id);
    if (!user) {
      return callback({
        code: grpc.status.NOT_FOUND,
        details: "User not found",
      });
    }
    callback(null, user);
  },

  // Unary with pagination
  listUsers: (call, callback) => {
    const page = call.request.page || 1;
    const limit = call.request.limit || 20;
    const offset = (page - 1) * limit;

    const userArray = Array.from(users.values());
    const paginatedUsers = userArray.slice(offset, offset + limit);

    callback(null, {
      users: paginatedUsers,
      total: userArray.length,
      page: page,
    });
  },

  // Unary — create with auto ID
  createUser: (call, callback) => {
    const id = String(userIdCounter++);
    const user = {
      id,
      email: call.request.email,
      first_name: call.request.first_name,
      last_name: call.request.last_name,
      role: call.request.role,
      created_at: Date.now(),
      updated_at: Date.now(),
    };
    users.set(id, user);
    callback(null, user);
  },

  // Server streaming — push all users to client
  streamUsers: (call) => {
    Array.from(users.values()).forEach((user) => {
      call.write(user);
    });
    call.end();
  },

  // Client streaming — receive bulk data, respond once
  bulkCreateUsers: (call, callback) => {
    const createdUsers = [];

    call.on("data", (request) => {
      const id = String(userIdCounter++);
      const user = {
        id,
        email: request.email,
        first_name: request.first_name,
        last_name: request.last_name,
        role: request.role,
        created_at: Date.now(),
        updated_at: Date.now(),
      };
      users.set(id, user);
      createdUsers.push(user);
    });

    call.on("end", () => {
      callback(null, {
        users: createdUsers,
        total: createdUsers.length,
        page: 1,
      });
    });

    call.on("error", (err) => {
      callback(err);
    });
  },
};

// Start the server
const server = new grpc.Server();
server.addService(userProto.UserService.service, userServiceImpl);

server.bindAsync(
  "0.0.0.0:50051",
  grpc.ServerCredentials.createInsecure(),
  () => {
    console.log("gRPC server running on port 50051");
  },
);

Client Implementation

Unary, Server Streaming, and Client Streaming Calls

const grpc = require("@grpc/grpc-js");
const protoLoader = require("@grpc/proto-loader");
const path = require("path");

const packageDef = protoLoader.loadSync(path.join(__dirname, "user.proto"));
const userProto = grpc.loadPackageDefinition(packageDef).user.service;

const client = new userProto.UserService(
  "localhost:50051",
  grpc.credentials.createInsecure(),
);

// Unary call
client.getUser({ id: "123" }, (err, user) => {
  if (err) console.error(err);
  console.log("User:", user);
});

// Server streaming — receive multiple messages
const stream = client.streamUsers({});
stream.on("data", (user) => {
  console.log("Received user:", user);
});
stream.on("end", () => {
  console.log("Stream ended");
});

// Client streaming — send multiple messages, receive one response
const writeStream = client.bulkCreateUsers((err, response) => {
  if (err) console.error(err);
  console.log("Created users:", response.users.length);
});

writeStream.write({
  email: "user1@example.com",
  first_name: "John",
  last_name: "Doe",
});
writeStream.write({
  email: "user2@example.com",
  first_name: "Jane",
  last_name: "Smith",
});
writeStream.end();

Client with Deadlines and Metadata

// Always set deadlines in production
const deadline = new Date();
deadline.setSeconds(deadline.getSeconds() + 5); // 5-second deadline

const metadata = new grpc.Metadata();
metadata.add("x-request-id", "abc-123");
metadata.add("authorization", "Bearer token-here");

client.getUser({ id: "123" }, metadata, { deadline }, (err, user) => {
  if (err) {
    if (err.code === grpc.status.DEADLINE_EXCEEDED) {
      console.error("Request timed out");
    }
    return;
  }
  console.log("User:", user);
});

gRPC Status Codes Reference

CodeNameWhen to Use
0OKSuccess
3INVALID_ARGUMENTBad input from client
5NOT_FOUNDResource does not exist
6ALREADY_EXISTSDuplicate creation attempt
7PERMISSION_DENIEDAuthenticated but not authorized
13INTERNALUnexpected server error
14UNAVAILABLETransient failure, client should retry
16UNAUTHENTICATEDMissing or invalid credentials

Best Practices

✅ DO

  • Define .proto files first — they are your single source of truth.
  • Use clear, descriptive message and service naming (CreateUserRequest, not Req1).
  • Implement proper error handling with gRPC status codes on every handler.
  • Add metadata for request tracing (request ID, correlation ID).
  • Version your protobuf definitions — never break backwards compatibility.
  • Use server streaming for large datasets instead of massive unary responses.
  • Set deadlines on every client call.
  • Use TLS credentials in production (grpc.ServerCredentials.createSsl()).
  • Monitor gRPC metrics (latency, error rates, stream counts).

❌ DON'T

  • Use gRPC directly from browsers — use gRPC-Web or a REST gateway instead.
  • Reuse or reassign proto field numbers after they have been published.
  • Create deeply nested message types — keep messages flat and composable.
  • Ignore error status codes or return generic INTERNAL for all failures.
  • Send uncompressed large payloads — enable gzip with grpc.compression.
  • Skip TLS in production — always encrypt service-to-service traffic.
  • Use createInsecure() credentials outside of local development.
  • Expose internal implementation details in proto definitions.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.