Skip to main content

LangChain (Python)

Connect a LangChain agent to Civic using civic-mcp-client. The langchain() adapter returns tool schemas ready to bind to any ChatModel, with a helper for executing tool calls.

Prerequisites

  • Python 3.11+
  • A Civic account at app.civic.com with a configured toolkit
  • A Civic token and an LLM API key (e.g. Anthropic)

Installation

pip install "civic-mcp-client[langchain]" langchain-anthropic python-dotenv

Environment Variables

CIVIC_TOKEN=your-civic-token
ANTHROPIC_API_KEY=your-anthropic-key
Get Your Credentials

How to generate a Civic token and configure toolkit URL parameters

Connecting to Civic

Use CivicMCPClient with the langchain() adapter to get tool schemas, bind them to a model, then use execute_langchain_tool_call to execute tool calls from the model's response:

import asyncio
import os
from dotenv import load_dotenv
from civic_mcp_client import CivicMCPClient
from civic_mcp_client.adapters.langchain import execute_langchain_tool_call, langchain
from langchain_anthropic import ChatAnthropic
from langchain_core.messages import HumanMessage, ToolMessage

load_dotenv()

async def main():
client = CivicMCPClient(auth={"token": os.environ["CIVIC_TOKEN"]})
tool_schemas = await client.adapt_for(langchain())

model = ChatAnthropic(model="claude-sonnet-4-6").bind_tools(tool_schemas)

messages = [HumanMessage(content="What events do I have today?")]

while True:
response = await model.ainvoke(messages)
messages.append(response)

if not response.tool_calls:
break

for tool_call in response.tool_calls:
result = await execute_langchain_tool_call(client, tool_call)
messages.append(ToolMessage(
content=str(result),
tool_call_id=tool_call["id"],
))

print(messages[-1].content)
await client.close()

asyncio.run(main())

Production Configuration

Lock to a Toolkit

For production agents, scope to a specific toolkit:

client = CivicMCPClient(
auth={"token": os.environ["CIVIC_TOKEN"]},
civic_profile=os.environ.get("CIVIC_PROFILE_ID"),
)

When a profile is specified, the session is locked by default — the agent cannot switch toolkits or modify its own guardrails. This prevents prompt injection attacks from escaping the defined tool scope.

Reference Implementation

langchain-nexus-reference-implementation

Complete implementation with FastAPI chat UI, streaming responses, and production patterns

Next Steps