> ## Documentation Index
> Fetch the complete documentation index at: https://gproxy.leenhawk.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Gemini tool delivery

> Have a Gemini upstream deliver its reply through a function call, and wrap the context into the function's description, with magic strings.

This page describes how GPROXY makes a Gemini upstream deliver its reply in
the argument of a function call and turns that call back into ordinary text
for the client. A client turns the feature on with magic strings embedded in
the prompt and neither declares nor handles the function call itself.

The feature applies to `generateContent` and `streamGenerateContent` in the
Gemini protocol. A client request in OpenAI or Claude format that is routed to
one of these providers is converted to Gemini first, so it works there too,
and the client still receives a response in its own format.

## Magic strings

Embed these strings in any text of the prompt:

```text theme={null}
GPROXY_MAGIC_STRING_TRIGGER_TOOL_DELIVERY_5JY1C1F45GL7EQGEFG7V4N684PMA5K1TZKJN3SCTHPH0DFANW1UVULN854P94LAW
GPROXY_MAGIC_STRING_TRIGGER_TOOL_WRAP_BEGIN_KUT5IZMSFP80VXGOX8SU4YLG55XV8YDRFZH4L4UO06VU8OQ2FNDNCH62CKSIWUBV
GPROXY_MAGIC_STRING_TRIGGER_TOOL_WRAP_END_BYZIK9PA49IR99W6XJ6GXMQ044WYGX35ZRHDYLLJMVOG7G3RO6QKGTBCHCYKSQB2
```

| String | Effect |
| - | - |
| …`TOOL_DELIVERY_5JY1C1…` | Turns tool delivery on |
| …`TOOL_WRAP_BEGIN_KUT5IZ…` | Start of the wrapped range; also turns tool delivery on |
| …`TOOL_WRAP_END_BYZIK9…` | End of the wrapped range |

The strings are part of the protocol between the client and GPROXY and do not
change between versions.

## Enabling

Enable it in the provider `config`:

```json theme={null}
{ "config": { "enable_gemini_magic_tool_delivery": true } }
```

The channels that have this switch are `aistudio`, `vertex`, `vertexexpress`, `geminicli`, `antigravity` and `playground`. The console shows it as
"Enable Gemini magic tool delivery".

These channels remove the strings from a request whether the switch is on or
off. With the switch off, only the strings are removed and nothing else
changes. A body without a string is forwarded byte for byte; it is neither
parsed nor serialized again.

## Tool delivery

When the first string appears in a request, GPROXY removes it and adds one
function declaration and a configuration that forces the call:

```json theme={null}
{
  "tools": [{ "functionDeclarations": [{
    "name": "narrative",
    "description": "Deliver the complete scene narration.",
    "parameters": {
      "type": "OBJECT",
      "properties": { "text": { "type": "STRING", "description": "The full reply, nothing omitted." } },
      "required": ["text"]
    }
  }] }],
  "toolConfig": { "functionCallingConfig": { "mode": "ANY", "allowedFunctionNames": ["narrative"] } }
}
```

The position of the string makes no difference. The text around it stays
where it is.

In the response, a call of `narrative` is replaced by a text part that holds
the `text` argument. The other fields of that part, `thoughtSignature`
included, are kept, and thought parts are unchanged:

```json theme={null}
{ "functionCall": { "name": "narrative", "args": { "text": "…" } } }
```

```json theme={null}
{ "text": "…" }
```

The client sends this text back as history on the next turn, so the history
never contains a function call.

When the model writes the reply directly instead of calling the function, the
response is returned as it is.

## Context wrapping

The text between the second and third strings is moved out of
`systemInstruction` and `contents` into the function's description. Each
message becomes one section, in request order, with the system instruction
first:

```text theme={null}
Deliver the complete scene narration.

<message role="system">
…
</message>

<message role="user">
…
</message>
```

The rules:

* The text is cut at the exact position of the strings. When a string falls
  inside a message, the part outside the range stays in that message and the
  part inside the range moves into the description.
* A start without an end extends the range to the end of the request.
* Non-text parts in the range, such as images, stay where they are. Thought
  parts in the range are dropped.
* A part or a message left without text is removed.
* When no user message is left outside the range, GPROXY appends one user
  message: `Read the narrative tool's description, then call it with your reply.`

The first string is not needed when wrapping is used.

A wrapped request has no cacheable prefix, because the function declaration
precedes every message and changes on each turn.

## Limits

* When the client declares `tools` of its own, built-in tools such as
  `googleSearch` included, the feature is not applied and only the strings
  are removed.
* Gemini sends the arguments of a function call as a whole. With the feature
  on, the text of a streamed response arrives at once after the model has
  finished writing it, not incrementally.
* The protocol of the `playground` channel has no place for the calling mode,
  so the function is declared but not forced. For a reliable call, state in
  the prompt that the whole reply must be delivered through the `narrative`
  function.
* GPROXY adds no instruction to the prompt, apart from the appended user
  message described above.


## Related topics

- [Channel Catalogue](https://gproxy.leenhawk.com/reference/channel-catalog.md)
- [Providers & Credentials](https://gproxy.leenhawk.com/guides/providers.md)
- [POST /admin/api/mcp-tool-natures](https://gproxy.leenhawk.com/api-reference/management/post-adminapimcp-tool-natures.md)
- [GPROXY](https://gproxy.leenhawk.com/index.md)
- [GET /admin/api/mcp-tool-natures](https://gproxy.leenhawk.com/api-reference/management/get-adminapimcp-tool-natures.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.