MCP Tools

generate_code

Generates code that sends a request, using a code generator.

Overview

generate_code exports a saved request or a draft as code, the way Relay's Preview panel does. Get the request's id from list_requests and a generator's id from list_generators.

{
  "requestId": "5B2C8E41-9D3A-4F67-B0E8-1A4C7D2F6E95",
  "generatorId": "python-requests"
}

The code includes the auth the request inherits from its folders, collection, workspace or globals. Unless the user turns on Reveal Credentials in Generated Code in the MCP pane of Relay's settings, {{variable}} tokens stay as written and credentials and cookies are replaced with placeholders. With it on, the code includes everything Relay would send: the request's variables resolved, its credentials and the cookies stored for its URL. A WebSocket request needs a WebSocket generator, such as ws-raw, ws-node-ws or ws-python-websockets, whose code opens the connection with the request's handshake and prints the messages it receives.

Parameters

Prop

Type

Return Value

The result's text is the generated code itself. Its structured content adds details about it:

{
  "code": "import requests\n\nurl = 'https://api.example.com/v2/orders'\n…",
  "kind": "http",
  "language": "python",
  "fileExtension": "py",
  "generatorId": "python-requests",
  "variablesResolved": false,
  "missingVariables": ["sku"],
  "logs": [],
  "warnings": []
}

Prop

Type

Discussion

By default, the code holds no credentials: Relay hides them from every MCP tool. With Reveal Credentials in Generated Code turned on, the code contains real credentials: tokens, passwords and keys from the request's auth, and the stored OAuth 2.0 access token if there is one. Relay doesn't fetch or refresh a token to generate code. AWS Signature, OAuth 1.0 and Hawk requests are signed when the code is generated.

A draft that isn't tied to a workspace uses only global variables and global auth. Dynamic variables such as {{$guid}} stay as written, and scripts don't run.

The tool returns an error when no request has the requestId you pass, or when the generator fails or runs too long. When no generator has the generatorId you pass, or its kind doesn't match the request's, the message lists the generators that fit the request.

See Also