> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ocient.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Build an AI Agent with the Ocient MCP Server

> Build your own AI agent on the Ocient MCP Server. Connect with the MCP SDK, discover tools, execute SQL statements, and design a governed tool surface.

export const Python = "Python®";

export const Ocient = "Ocient®";

You can build your own AI agent that uses the {Ocient} MCP Server as its data layer. An AI agent combines a large language model (LLM) with a set of tools in a loop: the model decides which tool to invoke, your agent code executes the tool, and the model uses the result to decide the next step. The Ocient MCP Server supplies the tools, so your agent can explore schemas, execute SQL statements, and invoke the custom tools that you define in the database — without any database-specific code.

## Agent Interaction with the MCP Server

An agent interacts with the Ocient MCP Server in this sequence:

1. The agent connects to the server and requests the tool list. The server returns `execute_statement` and all discovered tools, system tools and user-defined tools, with their MCP names (such as `show_tables`), argument schemas, and descriptions.
2. The agent passes the tool definitions to the LLM together with the user request.
3. The LLM selects a tool and provides the arguments. The agent invokes the tool through the MCP session.
4. The server executes the tool in the Ocient System and returns the result, which the agent passes back to the LLM.
5. The loop repeats until the LLM produces a final answer.

Because the database stores the tool definitions, the tool descriptions in the `sys.mcp_tools` system catalog table act as prompts for the LLM. Clear, specific tool descriptions improve how reliably the model selects and uses your tools.

## Connect to the Server from Python

This example uses the MCP {Python} SDK to launch a local server with the Stdio transport, list the available tools, and execute a SQL statement.

```python Python theme={null}
import asyncio
import os

from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client

server_params = StdioServerParameters(
    command="uvx",
    args=["--from", "ocientmcp", "ocientmcp-stdio"],
    env={"OCIENT_DSN": os.environ["OCIENT_DSN"]},
)

async def main():
    async with stdio_client(server_params) as (read, write):
        async with ClientSession(read, write) as session:
            await session.initialize()

            tools = await session.list_tools()
            for tool in tools.tools:
                print(f"{tool.name}: {tool.description}")

            result = await session.call_tool(
                "execute_statement",
                {"statement": "SELECT COUNT(*) FROM analytics.trips"},
            )
            print(result.content)

asyncio.run(main())
```

To connect to a remote server instead, use the Streamable HTTP client from the same SDK and pass your API key as a Bearer token. For deployment details, see [Deployment Models](/ocient-mcp-server#deployment-models).

## Add the Agent Loop

To turn the connection into an agent, pass the tool definitions from `list_tools` to your LLM provider as its available tools, and execute each tool invocation that the model requests through `session.call_tool`. Most LLM provider SDKs and agent frameworks accept MCP tool definitions directly or with a thin conversion layer, and many agent frameworks manage this loop for you when you register an MCP server as a tool source.

Follow these practices in the loop:

* Return tool errors to the model. The `execute_statement` tool returns Ocient error messages as descriptive tool errors, and the messages are descriptive enough for the model to correct its SQL statement and retry.
* Bound the loop with a maximum number of tool invocations to prevent runaway execution.
* Log each tool invocation and its SQL text for auditing. Agent-driven queries also appear in the `sys.queries` and `sys.completed_queries` system catalog tables.

## Design a Governed Tool Surface

The most reliable agents work with a small set of purpose-built tools instead of open-ended SQL. Use these techniques to shape what your agent can do.

* Create task-specific tools — Encapsulate approved queries as user-defined MCP tools with the `CREATE MCP TOOL` SQL statement. The agent invokes the tools by name with typed arguments, which removes whole categories of SQL generation errors. For the complete syntax, see [Model Context Protocol Tools](/model-context-protocol-tools).
* Group tools into toolsets — Assign related tools to a toolset with the `TOOLSET` clause of the `CREATE MCP TOOL` SQL statement so that agents can work with a coherent set of capabilities, for example, a geospatial analysis toolset or a reporting toolset.
* Scope privileges — Connect the agent with a dedicated database user that has only the privileges the agent requires. Revoke the EXECUTE privilege on tools that the agent must not use. The `CALL MCP TOOL` SQL statement executes with the privileges of the caller, so the database enforces your access control on every invocation. For the MCP tool privileges, see [Data Control Language (DCL) Statement Reference](/data-control-language-dcl-statement-reference#model-context-protocol-tool-privileges).
* Control query priority — Assign a service class to the agent user to manage the workload impact of agent-driven queries. For details, see [Workload Management and Service Classes](/workload-management-and-service-classes).

### Related Links

[Ocient MCP Server](/ocient-mcp-server)

[Data Analytics Workflow Using the Ocient MCP Server](/data-analytics-workflow-using-the-ocient-mcp-server)

[Model Context Protocol Tools](/model-context-protocol-tools)

[Data Control Language (DCL) Statement Reference](/data-control-language-dcl-statement-reference)
