Custom Framework Agent

This guide demonstrates how to integrate a custom agentic framework with the Agentstr SDK, using the Google ADK as an example. By following these steps, you can bring your own framework and connect it to the Nostr ecosystem for advanced agent functionality.

Tip

This example is more advanced and requires knowledge of other agentic frameworks. If you are new to agent development, we recommend starting with the Hello World guide.

Step 1: Initialize Your Project

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

agentstr init custom_framework_agent

This sets up a custom_framework_agent directory with the basic structure for your agent.

Step 2: Update Environment Variables

Update the custom_framework_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: Modify the Agent Code

Open the main.py file in your project to review and customize the agent setup for Google ADK:

 1"""Custom Framework agent (Google ADK) - Bring your own framework."""
 2
 3from dotenv import load_dotenv
 4load_dotenv()
 5
 6import asyncio
 7import os
 8
 9from google.adk.agents import Agent
10from google.adk.models.lite_llm import LiteLlm
11from agentstr import NostrAgent, AgentCard, NostrAgentServer, NostrMCPClient
12from agentstr.mcp.providers.google import to_google_tools
13from agentstr.agents.providers.google import google_chat_generator
14
15# Note: the NWC_CONN_STR environment variable is used by default for payment processing
16if os.getenv("NWC_CONN_STR") is None:
17    raise ValueError("NWC_CONN_STR environment variable is not set")
18
19# Note: make sure MCP_SERVER_PUBKEY is set
20if os.getenv("MCP_SERVER_PUBKEY") is None:
21    raise ValueError("MCP_SERVER_PUBKEY environment variable is not set")
22
23
24async def main():
25    # Create Nostr MCP client
26    nostr_mcp_client = NostrMCPClient(mcp_pubkey=os.getenv("MCP_SERVER_PUBKEY"))
27
28    # Convert tools to Google ADK tools
29    google_tools = await to_google_tools(nostr_mcp_client)
30
31    # Define Google agent
32    agent = Agent(
33        name="google_agent",
34        model=LiteLlm(
35            model=os.getenv("LLM_MODEL_NAME"),
36            api_base=os.getenv("LLM_BASE_URL").rstrip('/v1'),
37            api_key=os.getenv("LLM_API_KEY")
38        ),
39        instruction="You are a helpful assistant.",
40        tools=google_tools,
41    )
42
43    # Define agent callable
44    chat_generator = google_chat_generator(agent, [nostr_mcp_client])
45
46    # Create Nostr Agent
47    nostr_agent = NostrAgent(
48        agent_card=AgentCard(
49            name="Google Agent", 
50            description="A helpful assistant", 
51            skills=await nostr_mcp_client.get_skills(), 
52            satoshis=0,
53        ), 
54        chat_generator=chat_generator
55    )
56
57    # Create Nostr Agent Server
58    server = NostrAgentServer(nostr_mcp_client=nostr_mcp_client,
59                              nostr_agent=nostr_agent)
60
61    # Start server
62    await server.start()
63
64
65# Run the server
66if __name__ == "__main__":
67    asyncio.run(main())

Customize the agent name, description, and skills as needed. You can also modify the payment amount (satoshis) for using the agent.

Step 4: Update Test Client

Update custom_framework_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 Custom Framework Agent

Run your custom framework agent with tool calling capabilities:

python custom_framework_agent/main.py

Step 7: Test Your Agent

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

python custom_framework_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 Custom Framework Agent to the cloud for continuous operation. 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 custom_framework_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

  • More Framework Providers: Check out Custom Agent Frameworks for more information on other agentic framework providers.

  • 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.