Unlike end-user AI tools that add the Transform MCP server through a settings screen, Pydantic AI
connects to it from code. An MCPToolset loads the Transform MCP server’s
tools as native Pydantic AI tools, which you then pass to an agent through the agent’s toolsets argument. Because
Transform is a hosted remote MCP server, there is nothing to install or run locally: you point the toolset at the
server URL and authenticate with your Unstructured API key.
Requirements
You will need:
- Python 3.10 or later on your local development machine. To check, in your terminal, run
python --version or python3 --version. Install Python.
- An Unstructured API key, which the Transform MCP server uses as a bearer token. Get an API key.
- A chat model provider and its API key for the agent’s underlying model. For example, an Anthropic or OpenAI key.
Install pydantic-ai
In your terminal, install pydantic-ai:
The example below also uses httpx, which pydantic-ai installs as a dependency.
The Transform MCP server speaks the streamable HTTP transport and authenticates with your Unstructured API key sent as a
bearer token. Store your key in an environment variable rather than hard-coding it:
Create an MCPToolset that points at the server URL and passes your key through the auth argument, which sends it as
an Authorization: Bearer header:
MCPToolset gives you direct control of the connection, which you need in order to pass the bearer token. Pydantic AI
also offers a higher-level MCP capability that takes a server URL as a
one-line shortcut. Use MCPToolset when you need to configure authentication or the transport.
Register the toolset with an agent through its toolsets argument, then open the connection with async with agent
around your agent runs. Transform generates a signed upload URL for each local file, and expects the client to upload the
bytes, so this example gives the agent two small local tools: one to read a file’s metadata and one to upload its bytes.
The agent decides when to call each Transform tool and each local tool.
When you run this, the agent reads the file’s metadata, requests an upload URL, uploads the file, starts the transform,
polls for status until the job completes, and then fetches the results.
If your file is already reachable at a public HTTPS URL, you do not need the upload tools at all; the agent can
pass the URL directly to the Transform transform_files tool. The upload tools shown above are only needed for files
on your local machine.
Parse your source files
Parsing requests have the following limits:
- Each file must be of a supported file type.
- Each file must be 50 MB or less in size.
- Each request must have 10 files or fewer.
- Only 5 requests can be running at a time.
The Transform MCP server is designed to report these limits back to the agent through its tool responses. Because of
this, your agent should notify you whenever it encounters a file that exceeds 50 MB in size, and it should formulate
strategies to send requests that are 10 files or fewer and not cause more than 5 requests to be running at a time. You
can reinforce this behavior in your agent’s system prompt.
Authenticate each user with their own API key
The bearer-token approach shown above is the simplest and works well for scripts, backends, and shared-service setups.
In a multi-user application where each user has their own Unstructured credentials, build the toolset per run so that
each run authenticates as the right user. Use the @agent.toolset decorator
to read the user’s token from the run’s dependencies.
The auth argument also accepts the literal string 'oauth' to enable an interactive OAuth flow instead of a static
token. See the MCPToolset documentation for details.
Troubleshooting
401 Unauthorized on tool calls. Confirm the UNSTRUCTURED_API_KEY environment variable is set and that you are
passing it to MCPToolset through the auth argument.
McpError: Session terminated or 404 Not Found when connecting. Verify the server URL is exactly
https://mcp.transform.unstructured.io, with no /mcp path suffix, and check that your network allows outbound HTTPS
to mcp.transform.unstructured.io.
ModuleNotFoundError: pydantic_ai. Re-run pip install -U pydantic-ai in the same Python environment that runs
your agent.
ImportError for MCPToolset. Upgrade to a recent version of pydantic-ai with pip install -U pydantic-ai.
RuntimeError about the event loop. agent.run(...) and async with agent are async. Call them from an async
function (for example, via asyncio.run(main())) rather than at the top level of a script.
Next steps
- Control Transform file parsing output: Control how the Unstructured Transform MCP server instructs Transform to partition, enrich, chunk, and embed the data based on your files.
- Control Transform generated sample code: Control how the Unstructured Transform MCP server generates sample curl or Python code that demonstrates how to use Transform to partition, enrich, chunk, and embed the data based on your files.
Questions? Need help?