Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150---title: code_modesidebarTitle: code_mode---
# `fastmcp.experimental.transforms.code_mode`
## Classes
### `SandboxProvider` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/experimental/transforms/code_mode.py#L138" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Interface for executing LLM-generated Python code in a sandbox.
WARNING: The ``code`` parameter passed to ``run`` contains untrusted,LLM-generated Python. Implementations MUST execute it in an isolatedsandbox — never with plain ``exec()``. Use ``MontySandboxProvider``(backed by ``pydantic-monty``) for production workloads.
**Methods:**
#### `run` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/experimental/transforms/code_mode.py#L147" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonrun(self, code: str) -> Any```
### `MontySandboxProvider` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/experimental/transforms/code_mode.py#L175" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Sandbox provider backed by `pydantic-monty`.
**Args:**- `limits`: Resource limits for sandbox execution. Supported keys\:`max_feed_duration_secs` (float), `max_turn_duration_secs` (float),`max_memory` (int),`max_recursion_depth` (int), and `gc_interval` (int).Time, memory, and GC limits are optional; omit a key to disableit. Recursion depth defaults to Monty's standard maximum of 1,000.Duration limits measure sandbox execution time, excluding timewaiting on host callbacks. The feed limit covers the whole execution;the turn limit resets after each host round trip.Unsupported keys raise `ValueError` rather than being silentlyignored.
When the argument is omitted entirely, a conservative baselineis applied (``max_feed_duration_secs=30``, ``max_memory=100 MB``) sothe out-of-box configuration is not unbounded. Pass``limits=None`` to disable configurable time, memory, and GClimits, or a dict to set your own. Monty's standard recursionlimit still applies.
**Methods:**
#### `run` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/experimental/transforms/code_mode.py#L211" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonrun(self, code: str) -> Any```
### `Search` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/experimental/transforms/code_mode.py#L352" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Discovery tool factory that searches the catalog by query.
**Args:**- `search_fn`: Async callable ``(tools, query) -> matching_tools``.Defaults to BM25 ranking.- `name`: Name of the synthetic tool exposed to the LLM.- `default_detail`: Default detail level for search results.``"brief"`` returns tool names and descriptions only.``"detailed"`` returns compact markdown with parameter schemas.``"full"`` returns complete JSON tool definitions.- `default_limit`: Maximum number of results to return.The LLM can override this per call. ``None`` means no limit.
### `GetSchemas` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/experimental/transforms/code_mode.py#L434" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Discovery tool factory that returns schemas for tools by name.
**Args:**- `name`: Name of the synthetic tool exposed to the LLM.- `default_detail`: Default detail level for schema results.``"brief"`` returns tool names and descriptions only.``"detailed"`` renders compact markdown with parameter names,types, and required markers.``"full"`` returns the complete JSON schema.
### `GetTags` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/experimental/transforms/code_mode.py#L495" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Discovery tool factory that lists tool tags from the catalog.
Reads ``tool.tags`` from the catalog and groups tools by tag. Toolswithout tags appear under ``"untagged"``.
**Args:**- `name`: Name of the synthetic tool exposed to the LLM.- `default_detail`: Default detail level.``"brief"`` returns tag names with tool counts.``"full"`` lists all tools under each tag.
### `ListTools` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/experimental/transforms/code_mode.py#L562" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Discovery tool factory that lists all tools in the catalog.
**Args:**- `name`: Name of the synthetic tool exposed to the LLM.- `default_detail`: Default detail level.``"brief"`` returns tool names and one-line descriptions.``"detailed"`` returns compact markdown with parameter schemas.``"full"`` returns the complete JSON schema.
### `CodeMode` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/experimental/transforms/code_mode.py#L611" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Transform that collapses all tools into discovery + execute meta-tools.
Discovery tools are composable via the ``discovery_tools`` parameter.Each is a callable that receives catalog access and returns a ``Tool``.By default, ``Search`` and ``GetSchemas`` are included forprogressive disclosure: search finds candidates, get_schema retrievesparameter details, and execute runs code.
The ``execute`` tool is always present and provides a sandboxed Pythonenvironment with ``call_tool(name, params)`` in scope.
**Methods:**
#### `transform_tools` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/experimental/transforms/code_mode.py#L663" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythontransform_tools(self, tools: Sequence[Tool]) -> Sequence[Tool]```
#### `get_tool` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/experimental/transforms/code_mode.py#L666" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonget_tool(self, name: str, call_next: GetToolNext) -> Tool | None```