DSPy is an open-source Python framework for programming, rather than prompting, language models.
Unlike end-user AI tools that add the Transform MCP server through a settings screen, DSPy connects to it from code:
you load the Transform MCP server’s tools through the official MCP Python SDK and hand them to a DSPy agent such as
dspy.ReAct. Because Transform is a hosted remote MCP server, there is nothing to install or run locally: you point
the client at the server URL and authenticate with your Unstructured API key.
Requirements
Before you begin, you must have the following:
- 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.
- An OpenAI API key, or another model provider that DSPy supports, for the agent’s underlying chat model.
Install the packages
In your terminal, install DSPy, the MCP Python SDK, and httpx for the pre-signed upload and download requests:
This guide was verified against the pinned versions shown. Newer versions typically work, but check the
DSPy release notes if an API has changed.
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:
Open a session with the MCP Python SDK, then convert the server’s tools into DSPy tools with
dspy.Tool.from_mcp_tool:
The converted tools call back into the MCP session, so build and run your agent inside the two async with blocks.
Tools used after the session closes fail with a closed-connection error.
Add local file helpers for uploads, downloads, and waiting
Transform’s job lifecycle mixes MCP tool calls with two plain HTTP requests that are not MCP calls: an HTTP PUT of
the raw file bytes to a pre-signed upload_url, and an HTTP GET of the finished output from a pre-signed
download_url. An agent cannot perform raw HTTP requests through MCP tools alone. The full example in the next
section registers four small local functions as additional agent tools:
describe_local_file: Returns a file’s filename, content type, and exact size in bytes, which
request_file_upload_url requires.
upload_local_file: Sends the file bytes to the pre-signed upload_url with an HTTP PUT.
download_text: Retrieves the finished Markdown from the pre-signed download_url with an HTTP GET.
wait_seconds: Pauses between status checks. Each dspy.ReAct step consumes one of the agent’s max_iters
iterations, so pacing the polling keeps a multi-minute transform from exhausting the iteration budget.
Pre-signed URLs carry their own credentials in the URL itself. The helpers must not send the Authorization header on
the upload and download requests, or the storage service rejects them.
The following example builds a dspy.ReAct agent that can drive the full Transform job lifecycle. The agent requests
an upload URL, uploads the file through the local helper, starts the transform, polls for status with paced waits, and
downloads the Markdown output:
Because DSPy programs compose, the transformed Markdown can feed directly into a downstream dspy.Predict module, for
example one with a signature that extracts structured fields from the document text.
Run the agent
The example defaults to OpenAI as the chat model provider. Set your OpenAI key, place any PDF that is 50 MB or smaller
at ./sample.pdf, and run the program:
The agent works through the tool sequence and prints a summary such as the following. Transforms take from about 10
seconds to several minutes, depending on page count:
To use a different model provider, set the DSPY_MODEL environment variable to any
model string DSPy supports. For example, for Azure OpenAI, set
DSPY_MODEL="azure/<your-deployment-name>" along with the AZURE_API_KEY, AZURE_API_BASE, and AZURE_API_VERSION
environment variables.
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 the agent’s signature docstring, as the example above does for the file size limit.
Troubleshooting
401 Unauthorized or an invalid_token error on connect. Confirm the UNSTRUCTURED_API_KEY environment
variable is set and that the Authorization header is formatted as Bearer <your-unstructured-api-key>.
Session terminated or 404 when connecting. Verify the server URL is exactly
https://mcp.transform.unstructured.io, with no path appended.
- A closed-connection error when a tool runs. The MCP tools are bound to the session, so run the agent inside the
streamablehttp_client and ClientSession context managers, as the example does.
- The agent stops before the job finishes. Each ReAct step consumes one of
max_iters, and each poll cycle uses
two steps (wait_seconds plus check_job_status), so max_iters=50 covers roughly four minutes of
polling. For longer transforms, raise max_iters or increase the wait between checks.
- The upload or download request is rejected. The
upload_url and download_url values are pre-signed. Send those
two requests without the Authorization header.
Next steps
Questions? Need help?