Build a Coding Agent
Stage 2 of 7v1 · b7a24a84

Tool Declarations in Standard OpenRouter / OpenAI Format

Implement tool schema generation in JSON Schema format compatible with OpenRouter and OpenAI function calling APIs.

How Models Discover Available Tools

A language model cannot guess what functions you wrote in your program. For the model to call a tool, you must provide a structured definition conforming to the JSON Schema standard.

In OpenRouter and OpenAI APIs, available tools are passed inside a tools list where each entry follows this structure:

{
  "type": "function",
  "function": {
    "name": "tool_name",
    "description": "Clear explanation of what the tool accomplishes",
    "parameters": {
      "type": "object",
      "properties": {
        "param_1": { "type": "string", "description": "Parameter purpose" }
      },
      "required": ["param_1"]
    }
  }
}

With this schema, the model learns parameter names, expected data types, and required fields.

Input and Output Protocol

Your program reads JSON on stdin specifying the desired tool schema:

{
  "op": "tool_schema",
  "tool": "read_file"
}

And outputs compact JSON with keys in alphabetical order representing the tool declaration.

1. read_file

  • name: "read_file"
  • description: "Lee el contenido de un archivo con numeración de líneas y soporte de rangos."
  • parameters.properties:
    • path: {"type": "string", "description": "Ruta relativa del archivo"}
    • start_line: {"type": "integer", "description": "Línea inicial inclusiva (1-indexada)"}
    • end_line: {"type": "integer", "description": "Línea final inclusiva (1-indexada)"}
  • parameters.required: ["path"]

2. write_file

  • name: "write_file"
  • description: "Crea o sobreescribe un archivo en el espacio de trabajo."
  • parameters.properties:
    • path: {"type": "string", "description": "Ruta relativa del archivo a crear o sobreescribir"}
    • content: {"type": "string", "description": "Contenido completo a escribir en el archivo"}
  • parameters.required: ["content", "path"]

3. patch_file

  • name: "patch_file"
  • description: "Aplica una edición quirúrgica sustituyendo una ocurrencia única de texto."
  • parameters.properties:
    • path: {"type": "string", "description": "Ruta relativa del archivo a editar"}
    • old_str: {"type": "string", "description": "Texto exacto a buscar y reemplazar (debe ser único)"}
    • new_str: {"type": "string", "description": "Nuevo texto de reemplazo"}
  • parameters.required: ["new_str", "old_str", "path"]

Error Handling

If tool is not one of the three tools, if op is not "tool_schema", or if JSON is malformed:

  • Output error to stderr and exit with code 1.

Acceptance Criteria

  • Produces valid function calling tool schemas.
  • Accurately declares properties and required fields for each tool.
  • Rejects unknown tools and malformed input with exit code 1.