Skip to main content

Python Integration

This example demonstrates how to use the Corvic AI MCP protocol in Python to query a deployed agent that processes PDF-based enterprise data.

Use Case

We will ask a question that requires the Corvic agent to look up and reason through a NAICS (North American Industry Classification System) PDF document.

Prerequisites

  1. Complete the setup steps in this blog post.
  2. Upload the NAICS 2022 Manual PDF and deploy your agent on Corvic’s platform.
  3. Once deployed, copy the MCP endpoint and access token.

Integration Steps

  1. Use the mcp Python package to create an sse_client connection to the deployed agent.
  2. Pass the MCP endpoint and your API token in the request headers.
  3. Initialize the session and retrieve the list of available tools (Corvic currently supports a single tool: query).
  4. Call the query tool with your question via the query_content argument.
  5. Save the response to a markdown file for review.

Question Asked

Map the NAICS code 441228 from 2017 to the 2022 NAICS code.

Code Example

import mcp
import json

# Configure your Corvic MCP endpoint and token
MCP_ENDPOINT = "<<MCP_ENDPOINT>>"
CORVIC_API_TOKEN = "<<YOUR_CORVIC_API_TOKEN>>"

# Create SSE client connection
headers = {
    "Authorization": f"Bearer {CORVIC_API_TOKEN}",
    "Content-Type": "application/json"
}

# Initialize MCP client
async with mcp.ClientSession(
    transport=mcp.SSEClientTransport(MCP_ENDPOINT, headers=headers)
) as session:
    # Initialize the session
    init_result = await session.initialize()
    
    # List available tools
    tools = await session.list_tools()
    print(f"Available tools: {[tool.name for tool in tools.tools]}")
    
    # Call the query tool
    query_result = await session.call_tool(
        "query",
        arguments={
            "query_content": "Map the NAICS code 441228 from 2017 to the 2022 NAICS code."
        }
    )
    
    # Extract and save response
    response_text = query_result.content[0].text if query_result.content else ""
    
    # Save to markdown file
    with open("naics_mapping_result.md", "w") as f:
        f.write(response_text)
    
    print("Response saved to naics_mapping_result.md")

Response

The response will look as below. The mapping as well as the relevant section of the document is retrieved by the Corvic agent.
NAICS Mapping Result

Notes

  • The query_content argument is required.
  • Replace the placeholder token with your actual Corvic API token.
  • You can save or manipulate the response as needed (e.g., convert to HTML, display in UI, etc).

Resources