OpenAI Integration

async agentstr.mcp.openai.to_openai_tools(nostr_mcp_client: NostrMCPClient) list[FunctionTool][source]

Convert tools from the MCP client to OpenAI tools.

Parameters:

nostr_mcp_client – An instance of NostrMCPClient to fetch tools from.

Returns:

A list of OpenAI FunctionTool objects that wrap the MCP tools.

This module provides integration with OpenAI tools, enabling conversion between MCP tools and OpenAI’s tool format.

Usage Example

 1from dotenv import load_dotenv
 2
 3load_dotenv()
 4
 5import os
 6
 7from agents import Runner, Agent, AsyncOpenAI, OpenAIChatCompletionsModel
 8from agentstr import ChatInput, NostrAgentServer, NostrMCPClient
 9from agentstr.mcp.openai import to_openai_tools
10
11# Create Nostr MCP client
12nostr_mcp_client = NostrMCPClient(relays=os.getenv("NOSTR_RELAYS").split(","),
13                                  private_key=os.getenv("EXAMPLE_OPENAI_AGENT_NSEC"),
14                                  mcp_pubkey=os.getenv("EXAMPLE_MCP_SERVER_PUBKEY"),
15                                  nwc_str=os.getenv("MCP_CLIENT_NWC_CONN_STR"))
16
17async def agent_server():
18    # Define tools
19    openai_tools = await to_openai_tools(nostr_mcp_client)
20
21    for tool in openai_tools:
22        print(f'Found {tool.name}: {tool.description}')
23
24    # Define OpenAI agent
25    agent = Agent(
26        name="OpenAI Agent",
27        instructions="You are a helpful assistant.",
28        model=OpenAIChatCompletionsModel(
29            model=os.getenv("LLM_MODEL_NAME"),
30            openai_client=AsyncOpenAI(
31                base_url=os.getenv("LLM_BASE_URL"),
32                api_key=os.getenv("LLM_API_KEY"),
33            )
34        ),
35        tools=openai_tools,
36    )
37
38    # Define agent callable
39    async def agent_callable(input: ChatInput) -> str:
40        result = await Runner.run(agent, input=input.messages[-1])
41        return result.final_output
42
43    # Create Nostr Agent Server
44    server = NostrAgentServer(nostr_mcp_client=nostr_mcp_client,
45                              agent_callable=agent_callable)
46
47    # Start server
48    await server.start()
49
50
51if __name__ == "__main__":
52    import asyncio
53    asyncio.run(agent_server())