# Python MCP Client

## Install

```bash
pip install fastmcp
```

---

## Connect

```python
import asyncio
from fastmcp import Client

GATEWAY_URL = "https://gateway.mewcp.com/personal/mcp"
MEWCP_API_KEY = "API_KEY"

headers = {"Authorization": f"Bearer {MEWCP_API_KEY}"}

async def main():
    client = Client(GATEWAY_URL, headers=headers)

    async with client:
        await client.ping()

        tools = await client.list_tools()
        print("Available tools:", [t.name for t in tools])
        # -> ["search", "get_schema", "list_accounts", "call_tool"]

asyncio.run(main())
```

Every app you have connected is reachable through this one endpoint. You get four tools rather than the tools of each app, and you use them to reach the rest.

---

## Find a tool

```python
found = await client.call_tool("search", {"query": "calendar events"})
# each hit carries the server_maskedId and tool_name you need next

schema = await client.call_tool(
    "get_schema",
    {"tools": [{"server_maskedId": "google-calendar", "tool_name": "list_events"}]},
)
```

---

## Run a tool

```python
result = await client.call_tool(
    "call_tool",
    {
        "server_maskedId": "google-calendar",
        "tool_name": "list_events",
        "args": {
            "calendar_id": "primary",
            "time_min": "2026-01-15T00:00:00Z",
            "time_max": "2026-01-16T00:00:00Z",
        },
    },
)
print(result)
```

If your code already knows which tool it wants, call `call_tool` directly. `search` and `get_schema` exist for agents that have to work it out at runtime.

---

## Choosing an account

When you have connected more than one account for the same app, list them and pass the one you want as `alias`:

```python
accounts = await client.call_tool("list_accounts", {"provider": "google-gmail"})

result = await client.call_tool(
    "call_tool",
    {
        "server_maskedId": "google-gmail",
        "tool_name": "list_messages",
        "args": {"max_results": 10},
        "alias": "work",
    },
)
```

With a single connected account you can leave `alias` out.

---

## B2B: audience member credential routing

If you are managing audience member credentials via the [MewCP Auth API](/mewcp-auth), identify the end user with a header and MewCP resolves their stored credential automatically:

```python
headers = {
    "Authorization": f"Bearer {MEWCP_API_KEY}",
    "x-mewcp-enduser-id": current_user.id,
}
```

---

## Where to find your values

| Value          | Where to get it       |
| -------------- | --------------------- |
| `API_KEY`      | Dashboard -> API Keys |
| Connected apps | Dashboard -> Servers  |
