> For the complete documentation index, see [llms.txt](https://aesoperator.gitbook.io/aesoperator/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://aesoperator.gitbook.io/aesoperator/features/mcp-protocol.md).

# MCP Protocol

Anthropic proposed a novel standard for AI agents to access data sources called the Model Context Protocol (MCP) You can read more about it [from Anthropic's blog here](https://docs.anthropic.com/en/docs/model-context-protocol). Aesoperator implements MCP to give its operators direct access to your systems and data while maintaining security and control.

## How MCP Works in Aesoperator

```python
from aesoperator import MCPServer, MCPClient

# Create an MCP server to expose your data
server = MCPServer(
    name="github-connector",
    data_sources=["repos", "issues", "pull_requests"],
    auth_config={"type": "oauth2"}
)

# Connect Aesoperator as an MCP client
client = MCPClient(
    server_url="http://localhost:8000",
    credentials={"access_token": "..."}
)
```

### Key Components

1. **MCP Servers**: Expose your data sources through a standardized API

* Code repositories (Git, GitHub)
* Documentation (Google Drive, Notion)
* Databases (Postgres, MongoDB)
* Web apps (via Puppeteer)

2. **MCP Clients**: AI agents that connect to MCP servers

* Aesoperator operators act as MCP clients
* Can access multiple data sources
* Maintain context across interactions

3. **Context Management**:

* Persistent memory across sessions
* Knowledge graph of relationships
* Semantic search capabilities

## Benefits of MCP in Aesoperator

1. **Universal Data Access**

* Single protocol for all data sources
* No custom integrations needed
* Standardized authentication

2. **Context Awareness**

* Operators maintain state across calls
* Can reference previous interactions
* Build knowledge over time

3. **Security & Control**

* Fine-grained access control
* Audit logging
* Rate limiting

## Example: GitHub Integration

```python
# Setup GitHub MCP server
github_server = MCPServer(
    name="github",
    repo="username/repo",
    access_token="..."
)

# Create Aesoperator task with MCP context
task = aesop.Task(
    name="code_review",
    mcp_context=[github_server],
    inputs={
        "pr_number": 123,
        "review_type": "security"
    }
)

# Operator can now access GitHub data
result = aesop.execute_task(task)
```

## Example: Database Integration

```python
# Setup Postgres MCP server
db_server = MCPServer(
    name="analytics_db",
    connection_string="postgresql://...",
    allowed_tables=["users", "events"]
)

# Analyze data with MCP context
task = aesop.Task(
    name="user_analysis",
    mcp_context=[db_server],
    inputs={
        "metric": "retention",
        "date_range": "last_30_days"
    }
)

result = aesop.execute_task(task)
```

## Available MCP Actions

```python
# Query data source
mcp_query(
    server: str,
    query: str,
    params: Dict = None
) -> Dict

# Update data
mcp_update(
    server: str,
    operation: str,
    data: Dict
) -> None

# Stream changes
mcp_subscribe(
    server: str,
    event_type: str,
    handler: Callable
) -> None
```

## Best Practices

1. **Security**

* Use minimal access permissions
* Rotate credentials regularly
* Monitor usage patterns

2. **Performance**

* Cache frequently accessed data
* Use efficient queries
* Implement rate limiting

3. **Reliability**

* Handle connection errors
* Implement retries
* Monitor server health

## Learn More

* [MCP Specification](https://mcp.dev/spec)
* [Building MCP Servers](https://github.com/banbera/gitbook/blob/master/docs/features/servers.md)
* [MCP Security Guide](https://github.com/banbera/gitbook/blob/master/docs/features/security.md)
* [Example Implementations](https://github.com/banbera/gitbook/blob/master/docs/features/examples.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://aesoperator.gitbook.io/aesoperator/features/mcp-protocol.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
