How to Connect to Live Amazon Athena Data from Claude Agent SDK (via CData Connect AI)

Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
Leverage CData Connect AI to enable Claude Agent SDK applications to securely read and take actions on your Amazon Athena data for you.

Claude Agent SDK is a powerful framework from Anthropic for building production-ready AI agents with stateful conversations, automatic context management, and native MCP (Model Context Protocol) tool integration. When combined with CData Connect AI, you can leverage Claude Agent SDK to build intelligent agents that interact with your Amazon Athena data in real-time through natural language queries. This article outlines the process of connecting to Amazon Athena using Connect AI and configuring a Claude Agent SDK application to interact with your Amazon Athena data.

CData Connect AI offers a dedicated cloud-to-cloud interface for connecting to Amazon Athena data. The CData Connect AI Remote MCP Server enables secure communication between Claude Agent SDK applications and Amazon Athena. This allows your agents to read from and take actions on your Amazon Athena data, all without the need for data replication to a natively supported database. With its inherent optimized data processing capabilities, CData Connect AI efficiently channels all supported SQL operations, including filters and JOINs, directly to Amazon Athena. This leverages server-side processing to swiftly deliver the requested Amazon Athena data.

In this article, we show how to configure a Claude Agent SDK application to conversationally explore (or Vibe Query) your data using natural language. With Connect AI you can build agents with access to live Amazon Athena data, plus hundreds of other sources.

About Amazon Athena Data Integration

CData provides the easiest way to access and integrate live data from Amazon Athena. Customers use CData connectivity to:

  • Authenticate securely using a variety of methods, including IAM credentials, access keys, and Instance Profiles, catering to diverse security needs and simplifying the authentication process.
  • Streamline their setup and quickly resolve issue with detailed error messaging.
  • Enhance performance and minimize strain on client resources with server-side query execution.

Users frequently integrate Athena with analytics tools like Tableau, Power BI, and Excel for in-depth analytics from their preferred tools.

To learn more about unique Amazon Athena use cases with CData, check out our blog post: https://www.cdata.com/blog/amazon-athena-use-cases.


Getting Started


Step 1: Configure Amazon Athena Connectivity for Claude Agent SDK

Connectivity to Amazon Athena from Claude Agent SDK applications is made possible through CData Connect AI Remote MCP. To interact with Amazon Athena data from your agent, we start by creating and configuring a Amazon Athena connection in CData Connect AI.

  1. Log into Connect AI, click Sources, and then click Add Connection
  2. Select "Amazon Athena" from the Add Connection panel
  3. Enter the necessary authentication properties to connect to Amazon Athena.

    Authenticating to Amazon Athena

    To authorize Amazon Athena requests, provide the credentials for an administrator account or for an IAM user with custom permissions: Set AccessKey to the access key Id. Set SecretKey to the secret access key.

    Note: Though you can connect as the AWS account administrator, it is recommended to use IAM user credentials to access AWS services.

    Obtaining the Access Key

    To obtain the credentials for an IAM user, follow the steps below:

    1. Sign into the IAM console.
    2. In the navigation pane, select Users.
    3. To create or manage the access keys for a user, select the user and then select the Security Credentials tab.

    To obtain the credentials for your AWS root account, follow the steps below:

    1. Sign into the AWS Management console with the credentials for your root account.
    2. Select your account name or number and select My Security Credentials in the menu that is displayed.
    3. Click Continue to Security Credentials and expand the Access Keys section to manage or create root account access keys.

    Authenticating from an EC2 Instance

    If you are using the CData Data Provider for Amazon Athena 2018 from an EC2 Instance and have an IAM Role assigned to the instance, you can use the IAM Role to authenticate. To do so, set UseEC2Roles to true and leave AccessKey and SecretKey empty. The CData Data Provider for Amazon Athena 2018 will automatically obtain your IAM Role credentials and authenticate with them.

    Authenticating as an AWS Role

    In many situations it may be preferable to use an IAM role for authentication instead of the direct security credentials of an AWS root user. An AWS role may be used instead by specifying the RoleARN. This will cause the CData Data Provider for Amazon Athena 2018 to attempt to retrieve credentials for the specified role. If you are connecting to AWS (instead of already being connected such as on an EC2 instance), you must additionally specify the AccessKey and SecretKey of an IAM user to assume the role for. Roles may not be used when specifying the AccessKey and SecretKey of an AWS root user.

    Authenticating with MFA

    For users and roles that require Multi-factor Authentication, specify the MFASerialNumber and MFAToken connection properties. This will cause the CData Data Provider for Amazon Athena 2018 to submit the MFA credentials in a request to retrieve temporary authentication credentials. Note that the duration of the temporary credentials may be controlled via the TemporaryTokenDuration (default 3600 seconds).

    Connecting to Amazon Athena

    In addition to the AccessKey and SecretKey properties, specify Database, S3StagingDirectory and Region. Set Region to the region where your Amazon Athena data is hosted. Set S3StagingDirectory to a folder in S3 where you would like to store the results of queries.

    If Database is not set in the connection, the data provider connects to the default database set in Amazon Athena.

  4. Click Save & Test
  5. Navigate to the Permissions tab in the Add Amazon Athena Connection page and update the User-based permissions.

Add a Personal Access Token

A Personal Access Token (PAT) is used to authenticate the connection to Connect AI from your Claude Agent SDK application. It is best practice to create a separate PAT for each service to maintain granularity of access.

  1. Click on the Gear icon () at the top right of the Connect AI app to open the settings page.
  2. On the Settings page, go to the Access Tokens section and click Create PAT.
  3. Give the PAT a name and click Create.
  4. The personal access token is only visible at creation, so be sure to copy it and store it securely for future use.

With the connection configured and a PAT generated, we are ready to connect to Amazon Athena data from your Claude Agent SDK application.

Step 2: Set Up Your Claude Agent SDK Application for CData Connect AI

Follow these steps to configure your Claude Agent SDK application to connect to CData Connect AI. You can use our pre-built agent chatbot as a starting point, available at https://github.com/CDataSoftware/connect-ai-claude-agent, or follow the instructions below to create your own.

Prerequisites

  1. Python 3.8 or higher installed on your system
  2. An Anthropic API key (obtain from https://console.anthropic.com/)
  3. Install the required dependencies:
    pip install claude-agent-sdk python-dotenv requests

Configuration Steps

  1. Create a new Python project directory and navigate to it:
    mkdir my-cdata-agent
    cd my-cdata-agent
  2. Set up your environment variables. Create a .env file in your project root with the following variables:
    ANTHROPIC_API_KEY=your_anthropic_api_key
    [email protected]
    CDATA_ACCESS_TOKEN=your_personal_access_token
    Replace your_anthropic_api_key with your Anthropic API key, [email protected] with your Connect AI email address, and your_personal_access_token with the Personal Access Token created in Step 1.

Building Your Claude Agent SDK Application

With the setup for CData Connect AI completed, we are ready to build our agent application. We will walk through the key components needed to create a production-ready AI agent with stateful conversations and native MCP tool integration. For this example, we will create a single Python file (agent.py), which includes the MCP client, agent chatbot, and interactive interface.

Imports and Environment Setup

To start, import the necessary libraries and load environment variables. We need the Claude Agent SDK for agent functionality, the requests library for HTTP communication with the MCP server, and standard Python libraries for async operations and authentication.


#!/usr/bin/env python3
import os
import json
import base64
import asyncio
from typing import Optional, Dict, Any
from functools import partial
import requests
from claude_agent_sdk import query, ClaudeSDKClient, ClaudeAgentOptions, tool, create_sdk_mcp_server
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

Creating the MCP Client

The MCPClient class handles communication with the CData Connect AI MCP server over HTTP. It manages authentication using Basic Auth (email and personal access token), sends JSON-RPC requests to the MCP server, and parses Server-Sent Events (SSE) responses. The client provides two key methods: list_tools() to discover available tools and call_tool() to execute MCP tools.


class MCPClient:
    """Client for interacting with CData Connect AI MCP server over HTTP."""

    def __init__(self, server_url: str, email: Optional[str] = None, access_token: Optional[str] = None):
        self.server_url = server_url.rstrip('/')
        self.session = requests.Session()

        # Set default headers for MCP JSON-RPC
        self.session.headers.update({
            'Content-Type': 'application/json',
            'Accept': 'application/json, text/event-stream'
        })

        if email and access_token:
            # Basic authentication: email:personal_access_token
            credentials = f"{email}:{access_token}"
            encoded_credentials = base64.b64encode(credentials.encode()).decode()
            self.session.headers.update({
                'Authorization': f'Basic {encoded_credentials}'
            })

    def _parse_sse_response(self, response_text: str) -> dict:
        """Parse Server-Sent Events (SSE) response."""
        for line in response_text.split('\n'):
            if line.startswith('data: '):
                data_json = line[6:]  # Remove 'data: ' prefix
                return json.loads(data_json)
        raise ValueError("No data found in SSE response")

    def list_tools(self) -> list:
        """Get available tools from the MCP server."""
        response = self.session.post(
            self.server_url,
            json={
                "jsonrpc": "2.0",
                "method": "tools/list",
                "params": {},
                "id": 1
            }
        )
        response.raise_for_status()
        result = self._parse_sse_response(response.text)
        return result.get("result", {}).get("tools", [])

    def call_tool(self, tool_name: str, arguments: dict) -> dict:
        """Call a tool on the MCP server."""
        response = self.session.post(
            self.server_url,
            json={
                "jsonrpc": "2.0",
                "method": "tools/call",
                "params": {
                    "name": tool_name,
                    "arguments": arguments
                },
                "id": 2
            }
        )
        response.raise_for_status()
        result = self._parse_sse_response(response.text)
        return result.get("result", {})

Building the Agent Chatbot Class

The MCPAgentChatbot class is the core of our application. It connects the MCP client to the Claude Agent SDK, creating a production-ready AI agent with stateful conversations and automatic context management. The class performs several key functions during initialization: it creates an MCP client instance, discovers available tools from the CData Connect AI MCP server, wraps each MCP tool for use with the Agent SDK, and creates an SDK-compatible MCP server.

Initializing the Agent and Loading Tools

When the MCPAgentChatbot is initialized, it connects to the CData Connect AI MCP server and retrieves all available tools. These tools are then wrapped to be compatible with the Claude Agent SDK's tool framework.


class MCPAgentChatbot:
    """
    Production-ready AI agent chatbot for CData Connect AI.
    Uses Claude Agent SDK for stateful conversations and automatic context management.
    """

    def __init__(self, mcp_server_url: str, email: Optional[str] = None, access_token: Optional[str] = None):
        self.mcp_client = MCPClient(mcp_server_url, email, access_token)

        # Load available tools from MCP server
        print("Connecting to CData Connect AI MCP server...")
        self.mcp_tools_list = self.mcp_client.list_tools()
        print(f"✓ Loaded {len(self.mcp_tools_list)} tools from MCP server")

        # Create custom MCP tool wrappers for the Agent SDK
        self.agent_tools = self._create_agent_tools()

        # Create MCP server for Agent SDK
        self.mcp_server = create_sdk_mcp_server(
            name="cdata_connect",
            tools=self.agent_tools
        )

Creating Tool Wrappers for the Agent SDK

The _create_agent_tools() method transforms MCP tool definitions into Agent SDK-compatible tools. For each tool discovered from the MCP server, it creates a wrapper using the @tool decorator with the tool's name, description, and input schema. The _tool_handler() method is called whenever a tool is executed - it forwards the request to the MCP server and formats the response for the Agent SDK.


    async def _tool_handler(self, tool_name: str, args: Dict[str, Any]) -> Dict[str, Any]:
        """Call the MCP tool and return results."""
        result = self.mcp_client.call_tool(tool_name, args)
        return {
            "content": [{
                "type": "text",
                "text": json.dumps(result, indent=2)
            }]
        }

    def _create_agent_tools(self) -> list:
        """Create Agent SDK tool wrappers for MCP tools."""
        agent_tools = []
        for tool_info in self.mcp_tools_list:
            tool_name = tool_info.get("name")
            tool_description = tool_info.get("description", "")
            tool_schema = tool_info.get("inputSchema", {})

            agent_tool = tool(
                name=tool_name,
                description=tool_description,
                input_schema=tool_schema
            )(partial(self._tool_handler, tool_name))

            agent_tools.append(agent_tool)
        return agent_tools

Managing Stateful Conversation Sessions

One of the key features of the Claude Agent SDK is its ability to maintain stateful conversations with automatic context management. The create_session() method creates a ClaudeSDKClient instance configured with a system prompt, access to the MCP tools, and permission settings. The chat_session() method sends a user message and retrieves the agent's response, maintaining conversation context automatically.


    def create_session(self) -> ClaudeSDKClient:
        """Create a stateful conversation session."""
        options = ClaudeAgentOptions(
            system_prompt="You are a helpful assistant with access to Amazon Athena data through CData Connect AI.",
            mcp_servers={"cdata_connect": self.mcp_server},
            permission_mode="bypassPermissions"
        )
        return ClaudeSDKClient(options=options)

    async def chat_session(self, client: ClaudeSDKClient, user_message: str) -> str:
        """Send a message in a stateful session."""
        await client.query(user_message)
        async for message in client.receive_response():
            if hasattr(message, 'result'):
                return str(message.result)
        return ""

Creating the Interactive Interface

The main() function brings everything together to create an interactive chatbot. It loads credentials from environment variables, initializes the MCPAgentChatbot, creates a stateful session, and starts an interactive loop where users can chat with the agent. The session is managed using Python's async context manager (async with), which ensures proper cleanup of resources and API connections.


async def main():
    """Run the agent in interactive mode."""
    MCP_SERVER_URL = "https://mcp.cloud.cdata.com/mcp/"
    CDATA_EMAIL = os.environ.get("CDATA_EMAIL")
    CDATA_ACCESS_TOKEN = os.environ.get("CDATA_ACCESS_TOKEN")

    print("=== CData Connect AI Agent ===\n")

    chatbot = MCPAgentChatbot(MCP_SERVER_URL, CDATA_EMAIL, CDATA_ACCESS_TOKEN)
    client = chatbot.create_session()

    async with client:
        while True:
            user_input = input("You: ").strip()
            if user_input.lower() in ['quit', 'exit', 'q']:
                break
            response = await chatbot.chat_session(client, user_input)
            print(f"\nAssistant: {response}\n")


if __name__ == "__main__":
    asyncio.run(main())

Running Your Agent

  1. Save all the code sections above into a single file named agent.py in your project directory. Alternatively, you can download the complete example from https://github.com/CDataSoftware/connect-ai-claude-agent.
  2. Run your agent application:
    python agent.py
  3. Start interacting with your Amazon Athena data through natural language queries. Your agent now has access to your Amazon Athena data through the CData Connect AI MCP Server.

Step 3: Build Intelligent Agents with Live Amazon Athena Data Access

With your Claude Agent SDK application configured and connected to CData Connect AI, you can now build sophisticated agents that interact with your Amazon Athena data using natural language. The MCP integration provides your agents with powerful data access capabilities.

Available MCP Tools for Your Agent

Your Claude Agent SDK application has access to the following CData Connect AI MCP tools:

  • queryData: Execute SQL queries against connected data sources and retrieve results
  • getCatalogs: Retrieve a list of available connections from CData Connect AI
  • getSchemas: Retrieve database schemas for a specific catalog
  • getTables: Retrieve database tables for a specific catalog and schema
  • getColumns: Retrieve column metadata for a specific table
  • getProcedures: Retrieve stored procedures for a specific catalog and schema
  • getProcedureParameters: Retrieve parameter metadata for stored procedures
  • executeProcedure: Execute stored procedures with parameters

Key Features of Claude Agent SDK

The Claude Agent SDK provides several production-ready features that make it ideal for building AI agents:

  • Automatic Context Management: No manual conversation history tracking needed - the SDK handles context automatically
  • Stateful Conversations: Maintain context across multiple turns for natural, flowing interactions
  • Native Tool Integration: Tools are first-class citizens with automatic calling lifecycle and error handling
  • Context Compaction: Automatic context compaction when approaching token limits
  • Async/Await Architecture: Modern Python async patterns for better performance
  • Permission Controls: Fine-grained control over tool permissions and execution

Example Use Cases

Here are some examples of what your Claude Agent SDK applications can do with live Amazon Athena data access:

  • Data Analysis Agent: Build an agent that analyzes trends, patterns, and anomalies in your Amazon Athena data
  • Report Generation Agent: Create agents that generate custom reports based on natural language requests
  • Data Quality Agent: Develop agents that monitor and validate data quality in real-time
  • Business Intelligence Agent: Build agents that answer complex business questions by querying multiple data sources
  • Interactive Chatbot: Create chatbots that can answer questions about your Amazon Athena data in natural language
  • Automated Workflow Agent: Create agents that trigger actions based on data conditions in Amazon Athena

Testing Your Agent

Once your agent is running, you can interact with it through natural language queries. For example:

  • "Show me all customers from the last 30 days"
  • "What are the top performing products this quarter?"
  • "Analyze sales trends and identify anomalies"
  • "Generate a summary report of active projects"
  • "Find all records that match specific criteria"

Your Claude Agent SDK application will automatically translate these natural language queries into appropriate SQL queries and execute them against your Amazon Athena data through the CData Connect AI MCP Server, providing real-time insights without requiring users to write complex SQL or understand the underlying data structure.

Advanced Configuration

Programmatic Usage

You can also use the Claude Agent SDK programmatically for one-off queries or integration into larger applications:

One-off Queries

from claude_agent_sdk import query, ClaudeAgentOptions

async def single_query():
    options = ClaudeAgentOptions(
        system_prompt="You are a data analyst assistant.",
        mcp_servers={"cdata_connect": mcp_server},
        permission_mode="bypassPermissions"
    )

    response = await query(
        prompt="What data sources are available?",
        options=options
    )

    async for message in response:
        if hasattr(message, 'result'):
            print(message.result)

Stateful Multi-Turn Conversations

async def conversation():
    client = chatbot.create_session()

    async with client:
        # First query
        response1 = await chatbot.chat_session(client, "List my data sources")
        print(response1)

        # Follow-up query with context
        response2 = await chatbot.chat_session(client, "Tell me more about the first one")
        print(response2)

Permission Modes

Claude Agent SDK offers different permission modes for tool execution:

  • default: Prompts user for approval before executing tools
  • acceptEdits: Automatically accepts edit operations
  • bypassPermissions: Auto-approves all tools (recommended for CLI applications)
  • plan: Agent plans actions before execution

Get CData Connect AI

To get live data access to 300+ SaaS, Big Data, and NoSQL sources directly from your Claude Agent SDK applications, try CData Connect AI today!

Ready to get started?

Learn more about CData Connect AI or sign up for free trial access:

Free Trial