Metadata-Version: 2.4
Name: swarmauri_toolkit_mcpclient
Version: 0.1.0
Summary: Discover and call Model Context Protocol server tools from Swarmauri agents.
License-Expression: Apache-2.0
License-File: LICENSE
License-File: NOTICE
Keywords: swarmauri,toolkit,agents,mcp,model-context-protocol,tool-discovery,stdio,streamable-http
Author: Jacob Stewart
Author-email: jacob@swarmauri.com
Requires-Python: >=3.10,<3.15
Classifier: Development Status :: 1 - Planning
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Requires-Dist: anyio (>=4.7,<5)
Requires-Dist: httpx (>=0.28.1,<1)
Requires-Dist: mcp (>=1.30,<2)
Requires-Dist: pydantic (>=2.11,<3)
Requires-Dist: swarmauri_base (>=0.11.0)
Project-URL: Repository, https://github.com/swarmauri/swarmauri-sdk
Description-Content-Type: text/markdown

# swarmauri_toolkit_mcpclient

<!-- readme-standards:begin branding -->
<p align="center"><a href="https://github.com/swarmauri/swarmauri-sdk"><img src="https://raw.githubusercontent.com/swarmauri/swarmauri-sdk/master/assets/swarmauri.brand.theme.svg" alt="Swarmauri SDK" width="300"></a></p>
<p align="center"><strong>swarmauri_toolkit_mcpclient</strong></p>
<p align="center">Discover and call Model Context Protocol server tools from Swarmauri agents.</p>
<p align="center">
<a href="https://pypi.org/project/swarmauri_toolkit_mcpclient/"><img alt="pypi: swarmauri_toolkit_mcpclient unpublished" src="https://img.shields.io/static/v1?label=pypi&amp;message=swarmauri_toolkit_mcpclient%20unpublished&amp;labelColor=292421&amp;color=805500"></a>
<a href="https://github.com/swarmauri/swarmauri-sdk/blob/master/pkgs/50-community/swarmauri_toolkit_mcpclient/pyproject.toml"><img alt="Source version: 0.1.0" src="https://img.shields.io/static/v1?label=Source%20version&amp;message=0.1.0&amp;labelColor=292421&amp;color=994525"></a>
<a href="https://github.com/swarmauri/swarmauri-sdk/blob/master/pkgs/50-community/swarmauri_toolkit_mcpclient/pyproject.toml"><img alt="Python: &gt;=3.10,&lt;3.15" src="https://img.shields.io/static/v1?label=Python&amp;message=%3E%3D3.10%2C%3C3.15&amp;labelColor=292421&amp;color=994525"></a>
<a href="https://github.com/swarmauri/swarmauri-sdk/blob/master/pkgs/50-community/swarmauri_toolkit_mcpclient/pyproject.toml"><img alt="Layer: community" src="https://img.shields.io/static/v1?label=Layer&amp;message=community&amp;labelColor=292421&amp;color=994525"></a>
<a href="https://github.com/swarmauri/swarmauri-sdk/blob/master/pkgs/50-community/swarmauri_toolkit_mcpclient/README.md#lifecycle-and-migration"><img alt="Lifecycle: active" src="https://img.shields.io/static/v1?label=Lifecycle&amp;message=active&amp;labelColor=292421&amp;color=24654A"></a>
<a href="https://github.com/swarmauri/swarmauri-sdk/blob/master/pkgs/50-community/swarmauri_toolkit_mcpclient/LICENSE"><img alt="License: Apache-2.0" src="https://img.shields.io/static/v1?label=License&amp;message=Apache-2.0&amp;labelColor=292421&amp;color=994525"></a>
<a href="https://github.com/swarmauri/swarmauri-sdk/blob/master/pkgs/50-community/swarmauri_toolkit_mcpclient/NOTICE"><img alt="Notice: attribution" src="https://img.shields.io/static/v1?label=Notice&amp;message=attribution&amp;labelColor=292421&amp;color=994525"></a>
</p>
<p align="center">
<a href="https://pypi.org/project/swarmauri_toolkit_mcpclient/"><img alt="pypi downloads: swarmauri_toolkit_mcpclient: unpublished" src="https://img.shields.io/static/v1?label=pypi%20downloads&amp;message=swarmauri_toolkit_mcpclient%3A%20unpublished&amp;labelColor=292421&amp;color=805500"></a>
<a href="https://github.com/swarmauri/swarmauri-sdk/blob/master/pkgs/50-community/swarmauri_toolkit_mcpclient/README.md"><img alt="README hits: swarmauri_toolkit_mcpclient" src="https://hits.sh/github.com/swarmauri/swarmauri-sdk/pkgs/50-community/swarmauri_toolkit_mcpclient/README.md.svg?label=README%20hits&amp;labelColor=292421&amp;color=08704E"></a>
</p>
<p align="center">
<a href="https://github.com/swarmauri/swarmauri-sdk/actions/workflows/readme-standards.yml"><img alt="README standards: CI gate" src="https://img.shields.io/static/v1?label=README%20standards&amp;message=CI%20gate&amp;labelColor=292421&amp;color=67437E"></a>
<a href="https://github.com/swarmauri/swarmauri-sdk/blob/master/pkgs/50-community/swarmauri_toolkit_mcpclient/tests/i9n/test_toolkit.py"><img alt="Tests: integration tests" src="https://img.shields.io/static/v1?label=Tests&amp;message=integration%20tests&amp;labelColor=292421&amp;color=67437E"></a>
<a href="https://github.com/swarmauri/swarmauri-sdk/blob/master/pkgs/50-community/swarmauri_toolkit_mcpclient/README.md#certification-status"><img alt="Certification: not certified" src="https://img.shields.io/static/v1?label=Certification&amp;message=not%20certified&amp;labelColor=292421&amp;color=805500"></a>
<a href="https://github.com/swarmauri/swarmauri-sdk/blob/master/tools/readme/POLICY.md"><img alt="Governance: repository policy" src="https://img.shields.io/static/v1?label=Governance&amp;message=repository%20policy&amp;labelColor=292421&amp;color=67437E"></a>
<a href="https://github.com/swarmauri/swarmauri-sdk/blob/master/pkgs/50-community/swarmauri_toolkit_mcpclient/tests/i9n/test_toolkit.py"><img alt="Evidence: source and tests" src="https://img.shields.io/static/v1?label=Evidence&amp;message=source%20and%20tests&amp;labelColor=292421&amp;color=67437E"></a>
</p>
<p align="center"><a href="#overview">Overview</a> · <a href="#installation">Installation</a> · <a href="#quick-start">Quick Start</a> · <a href="#usage">Usage</a> · <a href="#public-api-and-import-surfaces">API</a> · <a href="#contributing">Contributing</a> · <a href="#governance">Governance</a> · <a href="#license">License</a></p>
<!-- readme-standards:end branding -->

---

## Overview

### Summary

Connect Swarmauri agents to tools exposed by a Model Context Protocol (MCP) server.

### Why This Package

The adapter turns advertised MCP tools into Swarmauri tool objects and reuses one client session across discovery and invocation.

### When to Use

Use this toolkit when your agent needs tools provided by a local subprocess or a remote MCP endpoint.

### Features

- Discover every page of server tools and restrict exposure with an optional allowlist.
- Connect over stdio or Streamable HTTP.
- Call tools synchronously or through the asynchronous wrapper.
- Return MCP content blocks, structured output and the tool error flag.

### Non-Goals

The scope is client-side tool discovery and invocation. MCP resources, prompts, and server hosting are outside this package's scope.

---

## Getting Started

### Prerequisites

Use Python 3.10 through 3.14 and a running HTTP MCP server or a local command that launches a stdio server. The agent example also uses the Swarmauri standard kernel, which requires Python 3.11 or later.

### Installation

#### Install with uv

<!-- readme-standards:begin installation.uv -->
`swarmauri_toolkit_mcpclient` is not declared published. From the source checkout:

```bash
uv pip install .
```
<!-- readme-standards:end installation.uv -->

#### Install with pip

<!-- readme-standards:begin installation.pip -->
`swarmauri_toolkit_mcpclient` is not declared published. From the source checkout:

```bash
python -m pip install .
```
<!-- readme-standards:end installation.pip -->

#### Install with Poetry

<!-- readme-standards:begin installation.poetry -->
`swarmauri_toolkit_mcpclient` is not declared published. From a separate Poetry project, replace the path with your checkout:

```bash
poetry add /path/to/swarmauri_toolkit_mcpclient
```
<!-- readme-standards:end installation.poetry -->

### Quick Start

#### Example

<!-- readme-standards:begin example.quick-start -->
Source: [docs/readme/quick_start.py](https://github.com/swarmauri/swarmauri-sdk/blob/master/pkgs/50-community/swarmauri_toolkit_mcpclient/docs/readme/quick_start.py).

```python
"""Discover and call a tool on the example stdio MCP server."""

from pathlib import Path
import sys

from mcp import StdioServerParameters

from swarmauri_toolkit_mcpclient import MCPToolkit


if __name__ == "__main__":
    fixture = (
        Path(__file__).resolve().parents[2] / "tests/i9n/fixture_server.py"
    )
    server = StdioServerParameters(command=sys.executable, args=[str(fixture)])
    with MCPToolkit.connect(server, allowed_tools={"sum_values"}) as toolkit:
        result = toolkit.get_tool_by_name("sum_values")(values=[2, 3])
        print(result["structuredContent"])
```
<!-- readme-standards:end example.quick-start -->

#### Expected Result

The stdio example prints a structured result containing `{'total': 5}`.

---

## Usage

### Common Workflows

<!-- readme-standards:begin example.usage -->
Source: [docs/readme/usage.py](https://github.com/swarmauri/swarmauri-sdk/blob/master/pkgs/50-community/swarmauri_toolkit_mcpclient/docs/readme/usage.py).

```python
"""Give a Swarmauri agent access to a remote weather tool."""

from swarmauri_standard.agents.ToolAgent import ToolAgent

from swarmauri_toolkit_mcpclient import MCPToolkit


def ask_weather(llm, conversation):
    """Use a configured tool model and conversation to ask for the weather."""
    with MCPToolkit.connect(
        "https://weather.example.com/mcp",
        allowed_tools={"get_weather"},
    ) as toolkit:
        agent = ToolAgent(llm=llm, conversation=conversation, toolkit=toolkit)
        return agent.exec(
            "What's the weather in Chicago?",
            llm_kwargs={"tool_choice": "auto"},
        )
```
<!-- readme-standards:end example.usage -->

### Configuration

Pass HTTP request headers with `headers`, configure the per-request timeout with `timeout`, and choose tools by original server name with `allowed_tools`.

**OAuth for Streamable HTTP**

Pass an MCP SDK OAuth provider as `auth` when opening the toolkit.
The provider authenticates both discovery and tool requests. Choose the provider
and token storage in the application before giving the toolkit to an agent.

```python
from mcp.client.auth.extensions.client_credentials import (
    ClientCredentialsOAuthProvider,
)

from swarmauri_toolkit_mcpclient import MCPToolkit


def connect_agent(endpoint, issuer, client_id, client_secret, storage):
    """Open an MCP session for a provisioned agent service principal."""
    auth = ClientCredentialsOAuthProvider(
        server_url=endpoint,
        issuer=issuer,
        client_id=client_id,
        client_secret=client_secret,
        storage=storage,
    )
    return MCPToolkit.connect(endpoint, auth=auth)
```

Use a separate service principal per autonomous agent when the MCP server must
authorize or audit them individually. Load each principal's credentials from the
application's secret store and pass its trusted authorization server `issuer`.
The SDK obtains a new access token when the current service token expires.

For user consent, pass `mcp.client.auth.OAuthClientProvider` with the application's
redirect and callback handlers. Set `timeout` to accommodate the consent window,
for example 300 seconds. The SDK uses authorization code with PKCE and renews
access during the connection when the issuer provides a refresh token. A delegated grant carries the user's permissions. A distinct
server-visible agent identity requires the issuer and server to support it.

Supply a separate SDK `TokenStorage` scope per agent, authorization issuer, MCP
resource, and delegated user or grant. The application controls persistence and
should coordinate concurrent connections that share a refresh-token grant. Use a
fresh provider per connection and HTTPS in production. A stored client
registration should be bound to its issuer. Keep credentials in the application
and turn consent or reauthorization into an application interaction.

### Error Handling

Check `isError` in the result for server-reported tool failures. Transport and protocol failures raise exceptions. An unknown allowlist entry fails discovery.

### Best Practices

Keep the connection context open for the entire agent run. Open a new context to rediscover the server's tool catalog.

---

## Package Boundary

### What It Owns

The toolkit owns the MCP client session, paginated tool discovery, name mapping, and submission of tool arguments and results.

### Public API and Import Surfaces

Import `MCPToolkit` or `MCPTool` from `swarmauri_toolkit_mcpclient`. The installed toolkit is also available under `swarmauri.toolkits.MCPToolkit`.

### Entry Points

<!-- readme-standards:begin entry-points -->
- `swarmauri.toolkits` → `MCPToolkit` → `swarmauri_toolkit_mcpclient:MCPToolkit`
<!-- readme-standards:end entry-points -->

### Injected Dependencies

Provide a Streamable HTTP URL or an MCP `StdioServerParameters` object. Supply HTTP authentication headers when required by the endpoint.

### Protocol Consumers

A Swarmauri `ToolAgent` consumes the toolkit through the existing toolkit and tool contracts. Direct callers can use `get_tool_by_name`.

### Extensions

The OpenAI and Anthropic converters can read each tool's original JSON Schema. Other converters use the Swarmauri parameter metadata.

---

## Operations and Readiness

### Operational Requirements

The stdio transport requires permission to launch the configured executable. Streamable HTTP requires network access to the configured endpoint.

### Health and Observability

Connection initialization and the first tool listing are completed before the context yields. Callers can record MCP tool outcomes and transport errors in their own logs.

### Security and Resource Limits

Use an allowlist to limit the tools agents can invoke, and configure a request timeout appropriate for the server.

### Compatibility

The toolkit uses MCP Python SDK 1.x and supports Python 3.10 through 3.14. Use `await tool.acall(...)` for direct calls from an asyncio application.

---

## Verification

### Tests

The package suite covers pagination, both transports, and an agent round trip with a mocked model response.

### Certification Status

Package verification is covered by repository CI and local integration tests.

### Evidence

See [the MCP integration tests](tests/i9n/test_toolkit.py) for the expected tool call, result, and error behavior.

---

## FAQ

### Why

A single toolkit lets an agent share the same MCP connection across tool calls.

### How

Call `MCPToolkit.connect` and pass the toolkit to the agent or call a tool by its discovered name.

### When

Connect before the agent starts. Exit the context when the agent finishes using server tools.

---

## Documentation and Related Packages

### More Documentation

See [the MCP Python SDK](https://github.com/modelcontextprotocol/python-sdk) for server and transport configuration.

### Related Packages

`swarmauri_base` provides the toolkit and tool contracts. `swarmauri_standard` provides `ToolAgent` and the schema converters.

### Project Links

<!-- readme-standards:begin project-links -->
- [Repository](https://github.com/swarmauri/swarmauri-sdk)
- [pypi: swarmauri_toolkit_mcpclient](https://pypi.org/project/swarmauri_toolkit_mcpclient/)
<!-- readme-standards:end project-links -->

---

## Contributing

### How to Contribute

Submit toolkit changes through the [Swarmauri contribution guide](../../../AGENTS.md).

### Development Setup

From the repository root, run `uv run --directory pkgs --package swarmauri_toolkit_mcpclient pytest 50-community/swarmauri_toolkit_mcpclient/tests`.

### Notes for Maintainers

Use the local MCP test fixture to validate changes to discovery or result translation without external model credentials.

---

## Governance

### Ownership and Decision Making

The Swarmauri SDK maintainers review changes to this community integration.

### Standards and Policies

This README follows the repository's [package README policy](../../../tools/readme/POLICY.md).

### Lifecycle and Migration

This initial community package is available from a repository checkout. Reconnect to rebuild callable tools from server metadata.

---

## License and Attribution

### License

<!-- readme-standards:begin license -->
Licensed under `Apache-2.0`. See [LICENSE](https://github.com/swarmauri/swarmauri-sdk/blob/master/pkgs/50-community/swarmauri_toolkit_mcpclient/LICENSE).
<!-- readme-standards:end license -->

### Notices

<!-- readme-standards:begin notices -->
See [NOTICE](https://github.com/swarmauri/swarmauri-sdk/blob/master/pkgs/50-community/swarmauri_toolkit_mcpclient/NOTICE) for attribution.
<!-- readme-standards:end notices -->

