PydanticAI Integration¶
- async agentstr.mcp.pydantic.to_pydantic_tools(nostr_mcp_client: NostrMCPClient) list[Tool] [source]¶
Convert tools from the MCP client to Pydantic tools.
- Parameters:
nostr_mcp_client – An instance of NostrMCPClient to fetch tools from.
- Returns:
A list of Pydantic tools that wrap the MCP tools.
This module provides integration with PydanticAI tools, enabling conversion between MCP tools and PydanticAI’s tool format.
Usage Example¶
1from dotenv import load_dotenv
2
3load_dotenv()
4
5import os
6
7from pydantic_ai import Agent
8from pydantic_ai.models.openai import OpenAIModel
9from pydantic_ai.providers.openai import OpenAIProvider
10
11from agentstr import ChatInput, NostrAgentServer, NostrMCPClient
12from agentstr.mcp.pydantic import to_pydantic_tools
13
14# Create Nostr MCP client
15nostr_mcp_client = NostrMCPClient(relays=os.getenv("NOSTR_RELAYS").split(","),
16 private_key=os.getenv("EXAMPLE_PYDANTIC_AGENT_NSEC"),
17 mcp_pubkey=os.getenv("EXAMPLE_MCP_SERVER_PUBKEY"),
18 nwc_str=os.getenv("MCP_CLIENT_NWC_CONN_STR"))
19
20async def agent_server():
21 # Define tools
22 pydantic_tools = await to_pydantic_tools(nostr_mcp_client)
23
24 for tool in pydantic_tools:
25 print(f'Found {tool.name}: {tool.description}')
26
27 # Define Pydantic agent
28 agent = Agent(
29 system="You are a helpful assistant.",
30 model=OpenAIModel(
31 os.getenv("LLM_MODEL_NAME"),
32 provider=OpenAIProvider(
33 base_url=os.getenv("LLM_BASE_URL"),
34 api_key=os.getenv("LLM_API_KEY"),
35 )
36 ),
37 tools=pydantic_tools,
38 )
39
40 # Define agent callable
41 async def agent_callable(input: ChatInput) -> str:
42 result = await agent.run(input.messages[-1])
43 return result.output
44
45 # Create Nostr Agent Server
46 server = NostrAgentServer(nostr_mcp_client=nostr_mcp_client,
47 agent_callable=agent_callable)
48
49 # Start server
50 await server.start()
51
52
53if __name__ == "__main__":
54 import asyncio
55 asyncio.run(agent_server())