Tool Calling Agent

This guide will walk you through creating an agent that can call external tools using the Agentstr SDK, expanding on the concepts from the Payment Enabled Agent example.

Step 1: Initialize Your Project

Start by initializing a new project if you haven’t already:

agentstr init tool_calling_agent

This sets up a tool_calling_agent directory with the necessary structure.

Step 2: Update .env file

Update the tool_calling_agent/.env file with your LLM information and NWC connection string.

LLM_BASE_URL=https://api.openai.com/v1
LLM_API_KEY=your-api-key
LLM_MODEL_NAME=gpt-3.5-turbo
NWC_CONN_STR=nostr+walletconnect://<your-nwc-connection-string>
MCP_SERVER_PUBKEY=<your-mcp-server-public-key>

Note

  • NWC_CONN_STR is the default environment variable for NWCRelay to enable Nostr Wallet Connect. You can override this by passing a connection string directly to the NostrClient constructor.

  • LLM_BASE_URL, LLM_API_KEY, and LLM_MODEL_NAME are the default environment variables for AgentstrAgent to enable LLM integration. You can override these by passing them directly to the agent constructor.

  • Replace <your-mcp-server-key> with the actual public key of a Nostr MCP server. If you’re testing locally, ensure an MCP server is also running locally.

  • You can follow the Creating an MCP Server guide to set up Nostr MCP server.

Step 3: Define Tools for Your Agent

Open tool_calling_agent/main.py and modify it to include tool calling functionality. You’ll need to integrate with the Model Context Protocol (MCP) for tool access.

 1"""Agentstr agent with payment processing and tool calling."""
 2
 3from dotenv import load_dotenv
 4load_dotenv()
 5
 6import asyncio
 7from agentstr import AgentstrAgent
 8import os
 9
10# Note: the NWC_CONN_STR environment variable is used by default for payment processing
11if os.getenv("NWC_CONN_STR") is None:
12    raise ValueError("NWC_CONN_STR environment variable is not set")
13
14# Note: make sure MCP_SERVER_PUBKEY is set
15if os.getenv("MCP_SERVER_PUBKEY") is None:
16    raise ValueError("MCP_SERVER_PUBKEY environment variable is not set")
17
18# Define the Nostr Agent Server
19async def main():
20    agent = AgentstrAgent(
21        name="PaymentEnabledAgent",
22        description="A simple Agentstr Agent with payment processing",
23        satoshis=0,  # 0 sats per message
24        nostr_mcp_pubkeys=[os.getenv("MCP_SERVER_PUBKEY")],
25    )
26    await agent.start()
27
28
29# Run the server
30if __name__ == "__main__":
31    asyncio.run(main())

Step 4: Update Test Client

Update tool_calling_agent/test_client.py to interact with your tool-enabled agent.

 1
 2from dotenv import load_dotenv
 3load_dotenv()
 4
 5import os
 6from agentstr import NostrClient, PrivateKey
 7
 8agent_pubkey = os.getenv("NOSTR_PUBKEY")
 9
10async def chat():
11    client = NostrClient(private_key=PrivateKey().bech32())
12    response = await client.send_direct_message_and_receive_response(
13        agent_pubkey,
14        "What's 59 + 88232?",
15    )
16    print(response.message)
17
18if __name__ == "__main__":
19    import asyncio
20    asyncio.run(chat())

Step 5: Start a Local Relay

Start a local Nostr relay for testing:

agentstr relay start

Keep this running in a separate terminal.

Step 6: Run Your Tool-Enabled Agent

Run your agent with tool calling capabilities:

python tool_calling_agent/main.py

Step 7: Test Your Agent

Use the test client to interact with your agent and see the tool listing response:

python tool_calling_agent/test_client.py

You should see a response indicating the answer to a math question. If you check the MCP Server logs, you’ll see that the addition tool was called by the agent.

Feel free to play around with the test client to ask the agent additional questions. Just note that any premium tools will require a Lightning payment.

Note

If you encounter connection issues with the MCP server, ensure the server is running and accessible. Refer to troubleshooting tips in the Hello World guide for general connectivity issues.

Step 8 (Optional): Deploy to the Cloud

Deploy your Tool Calling Agent to the cloud for continuous operation and public accessibility. Assuming you are already logged into the Agentstr CLI, follow these steps:

  1. Set your cloud provider:

    export AGENTSTR_PROVIDER=aws  # or gcp, azure
    
  2. Deploy the agent:

    agentstr deploy -f tool_calling_agent/deploy.yml
    

    This command packages your agent and deploys it to the specified cloud provider. Ensure your project directory structure is compatible with the deployment requirements.

For more information on cloud deployment and CI/CD, see the Cloud & CI/CD guide.

Next Steps

  • Expand Tool Integration: Explore more MCP tools and services to enhance your agent’s capabilities. Check out Model Context Protocol (MCP) for advanced integration techniques.

  • Dive into the API: Learn more about the capabilities of the SDK by exploring the Module Reference documentation.

  • Explore the Cookbook: Check out the Agentstr Cookbook for more advanced use cases and examples.