A lightweight library that converts MCP tools into Python tools (function-like objects).
Project description
mcputil
A lightweight library that converts MCP tools into Python tools (function-like objects).
Installation
pip install mcputil
Quickstart
(Check out the examples directory for runnable examples.)
Basic Usage
Given the following MCP server:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP(name="Basic", log_level="ERROR")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two numbers"""
return a + b
if __name__ == "__main__":
mcp.run(transport="stdio")
We can use mcputil to call the add tool easily:
import inspect
import mcputil
async def main():
async with mcputil.Client(
mcputil.Stdio(
command="python",
args=["/path/to/server.py"],
),
) as client:
tool: mcputil.Tool = (await client.get_tools())[0]
print(f"tool signature: {tool.name}{inspect.signature(tool)}")
output = await tool(a=1, b=2)
print(f"tool output: {output}")
# Output:
# tool signature: add(a: int, b: int) -> int
# tool output: 3
Progress Tracking
Given the following MCP server (see here):
from mcp.server.fastmcp import Context, FastMCP
from mcp.server.session import ServerSession
mcp = FastMCP(name="Progress")
@mcp.tool()
async def long_running_task(
task_name: str, ctx: Context[ServerSession, None], steps: int = 5
) -> str:
"""Execute a task with progress updates."""
for i in range(steps):
progress = (i + 1) / steps
await ctx.report_progress(
progress=progress,
total=1.0,
message=f"Step {i + 1}/{steps}",
)
return f"Task '{task_name}' completed"
if __name__ == "__main__":
mcp.run(transport="streamable-http")
python server.py
We can use mcputil to track the progress of the long_running_task tool:
import inspect
import mcputil
async def main():
async with mcputil.Client(
mcputil.StreamableHTTP(url="http://localhost:8000"),
) as client:
tool: mcputil.Tool = (await client.get_tools())[0]
print(f"tool signature: {tool.name}{inspect.signature(tool)}")
result: mcputil.Result = await tool.call(
"call_id_0", task_name="example-task", steps=5
)
async for event in result.events():
if isinstance(event, mcputil.ProgressEvent):
print(f"tool progress: {event}")
elif isinstance(event, mcputil.OutputEvent):
print(f"tool output: {event.output}")
# Output:
# tool signature: long_running_task(task_name: str, steps: int = 5) -> str
# tool progress: ProgressEvent(progress=0.2, total=1.0, message='Step 1/5')
# tool progress: ProgressEvent(progress=0.4, total=1.0, message='Step 2/5')
# tool progress: ProgressEvent(progress=0.6, total=1.0, message='Step 3/5')
# tool progress: ProgressEvent(progress=0.8, total=1.0, message='Step 4/5')
# tool progress: ProgressEvent(progress=1.0, total=1.0, message='Step 5/5')
# tool output: Task 'example-task' completed
Multiple MCP Servers
mcputil also supports managing multiple MCP servers using the Group class:
import inspect
import mcputil
async def main():
async with mcputil.Group(
math=mcputil.Stdio(
command="python",
args=["/path/to/server.py"],
),
progress=mcputil.StreamableHTTP(url="http://localhost:8000"),
) as group:
tools: list[mcputil.Tool] = await group.get_tools()
for tool in tools:
print(f"tool signature: {tool.name}{inspect.signature(tool)}")
if tool.name == "add":
output = await tool(a=1, b=2)
print(f"tool output: {output}\n")
elif tool.name == "long_running_task":
result: mcputil.Result = await tool.call(
"call_id_0", # non-empty call ID for tracking progress
task_name="example-task",
steps=5,
)
async for event in result.events():
if isinstance(event, mcputil.ProgressEvent):
print(f"tool progress: {event}")
elif isinstance(event, mcputil.OutputEvent):
print(f"tool output: {event.output}")
# Output:
# tool signature: add(a: int, b: int) -> int
# tool output: 3
#
# tool signature: long_running_task(task_name: str, steps: int = 5) -> str
# tool progress: ProgressEvent(progress=0.2, total=1.0, message='Step 1/5')
# tool progress: ProgressEvent(progress=0.4, total=1.0, message='Step 2/5')
# tool progress: ProgressEvent(progress=0.6, total=1.0, message='Step 3/5')
# tool progress: ProgressEvent(progress=0.8, total=1.0, message='Step 4/5')
# tool progress: ProgressEvent(progress=1.0, total=1.0, message='Step 5/5')
# tool output: Task 'example-task' completed
Additionally, you can also call tools by their names without fetching them first:
import inspect
import mcputil
async def main():
async with mcputil.Group(
math=mcputil.Stdio(
command="python",
args=["/path/to/server.py"],
),
progress=mcputil.StreamableHTTP(url="http://localhost:8000"),
) as group:
result: mcputil.Result = await group.call_tool("math", "add", a=1, b=2)
output = await result.output()
print(f"tool output: {output}\n")
result: mcputil.Result = await group.call_tool(
"progress",
"long_running_task",
call_id="call_id_0", # non-empty call ID for tracking progress
task_name="example-task",
steps=5,
)
async for event in result.events():
if isinstance(event, mcputil.ProgressEvent):
print(f"tool progress: {event}")
elif isinstance(event, mcputil.OutputEvent):
print(f"tool output: {event.output}")
# Output:
# tool output: 3
#
# tool progress: ProgressEvent(progress=0.2, total=1.0, message='Step 1/5')
# tool progress: ProgressEvent(progress=0.4, total=1.0, message='Step 2/5')
# tool progress: ProgressEvent(progress=0.6, total=1.0, message='Step 3/5')
# tool progress: ProgressEvent(progress=0.8, total=1.0, message='Step 4/5')
# tool progress: ProgressEvent(progress=1.0, total=1.0, message='Step 5/5')
# tool output: Task 'example-task' completed
Agent Framework Integration
Given the following MCP Server:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP(name="Basic")
@mcp.tool()
def get_weather(city: str) -> str:
"""Get the weather for a given city."""
return f"The weather in {city} is sunny."
if __name__ == "__main__":
mcp.run(transport="streamable-http")
LangChain
from langchain.tools import tool
from langchain.agents import create_agent
import mcputil
async def main():
async with mcputil.Client(
mcputil.StreamableHTTP(url="http://localhost:8000"),
) as client:
mcp_tools: list[mcputil.Tool] = await client.get_tools()
agent = create_agent(
"openai:gpt-5",
tools=[tool(t) for t in mcp_tools],
)
OpenAI Agents SDK
from agents import Agent, function_tool
import mcputil
async def main():
async with mcputil.Client(
mcputil.StreamableHTTP(url="http://localhost:8000"),
) as client:
mcp_tools: list[mcputil.Tool] = await client.get_tools()
agent = Agent(
name="Hello world",
instructions="You are a helpful agent.",
tools=[function_tool(t) for t in mcp_tools],
)
Pydantic AI
import mcputil
from pydantic_ai import Agent
async def main():
async with mcputil.Client(
mcputil.StreamableHTTP(url="http://localhost:8000"),
) as client:
mcp_tools: list[mcputil.Tool] = await client.get_tools()
agent = Agent(
"google-gla:gemini-2.5-flash",
deps_type=str,
tools=mcp_tools,
system_prompt="You are a helpful agent.",
)
CLI
mcputil also comes with a CLI for generating a file tree of all available tools from connected MCP servers, which helps with Code execution with MCP. Checkout out the example.
License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mcputil-0.6.1.tar.gz.
File metadata
- Download URL: mcputil-0.6.1.tar.gz
- Upload date:
- Size: 49.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4cf995fbb43d808a2054313bab6b0d532f1f8ac517fa47f015df442c3d1b8aea
|
|
| MD5 |
567754cec0357a3a8daaa0ed0fcf70df
|
|
| BLAKE2b-256 |
ef853c8996a248bb5e0eaf65c0298357a7c8e47965c34a86840e97ff6a805057
|
Provenance
The following attestation bundles were made for mcputil-0.6.1.tar.gz:
Publisher:
ci.yml on RussellLuo/mcputil
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mcputil-0.6.1.tar.gz -
Subject digest:
4cf995fbb43d808a2054313bab6b0d532f1f8ac517fa47f015df442c3d1b8aea - Sigstore transparency entry: 844828147
- Sigstore integration time:
-
Permalink:
RussellLuo/mcputil@6b9ab20f5d29777eb324eac5b51ca8a845863e3d -
Branch / Tag:
refs/tags/v0.6.1 - Owner: https://github.com/RussellLuo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@6b9ab20f5d29777eb324eac5b51ca8a845863e3d -
Trigger Event:
push
-
Statement type:
File details
Details for the file mcputil-0.6.1-py3-none-any.whl.
File metadata
- Download URL: mcputil-0.6.1-py3-none-any.whl
- Upload date:
- Size: 18.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0e99658645595378c467bd35ab9f850e93f038a17214d416b38328ee8e7a927
|
|
| MD5 |
5ae6b605249efa3e0c0e4123777f8525
|
|
| BLAKE2b-256 |
e960ff70e2d960b01c77654d363cf0589ea709e0c524a1696373853e8a0f59a9
|
Provenance
The following attestation bundles were made for mcputil-0.6.1-py3-none-any.whl:
Publisher:
ci.yml on RussellLuo/mcputil
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mcputil-0.6.1-py3-none-any.whl -
Subject digest:
d0e99658645595378c467bd35ab9f850e93f038a17214d416b38328ee8e7a927 - Sigstore transparency entry: 844828148
- Sigstore integration time:
-
Permalink:
RussellLuo/mcputil@6b9ab20f5d29777eb324eac5b51ca8a845863e3d -
Branch / Tag:
refs/tags/v0.6.1 - Owner: https://github.com/RussellLuo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@6b9ab20f5d29777eb324eac5b51ca8a845863e3d -
Trigger Event:
push
-
Statement type: