from langchain_mcp_adapters.client import MultiServerMCPClient
from langgraph.prebuilt import create_react_agent
from langchain_core.messages import HumanMessage, AIMessage, ToolMessage
import asyncio
# Initialize MCP client
client = MultiServerMCPClient(
{
"doctranslate": {
"transport": "streamable_http",
"url": "http://localhost:8000/mcp/",
},
}
)
# Get list of asynchronous tools
tools = asyncio.run(client.get_tools())
tools_dict = {tool.name: tool for tool in tools}
# Get required tools
get_credit_balance_tool = tools_dict.get("get_credit_balance")
calculate_credits_tool = tools_dict.get("calculate_credits")
# Create agent with asynchronous tools
credit_agent = create_react_agent(
model="openai:gpt-4",
tools=[get_credit_balance_tool, calculate_credits_tool],
prompt=(
"""
You are a Doctranslate credit calculation agent.
You have access to:
- get_credit_balance: Get user's credit balance (requires random_string parameter)
- calculate_credits: Calculate required credits to translate file (requires file_path parameter)
INSTRUCTIONS:
- When user requests to calculate required credits to translate a file, you need to call calculate_credits tool with parameter file_path="file_path"
- When user requests to get user's credit balance, you need to call get_credit_balance tool with parameter random_string="dummy"
- After completing the task, respond directly to the user with the result
"""
),
name="credit_agent",
)
async def get_agent_result(agent, messages):
result = await agent.ainvoke({"messages": messages})
for message in result["messages"]:
print(message)
return result
# Run main function asynchronously
if __name__ == "__main__":
asyncio.run(
get_agent_result(
credit_agent,
[HumanMessage(content="How many credits for new file translation?")],
)
)