-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathminimal_server.py
More file actions
74 lines (55 loc) · 1.84 KB
/
Copy pathminimal_server.py
File metadata and controls
74 lines (55 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
"""OpenANP Minimal Server Example.
Build a complete ANP Server with minimal code.
Run:
uvicorn examples.python.openanp_examples.minimal_server:app --port 8000
Generated Endpoints:
GET /agent/ad.json - Agent Description
GET /agent/interface.json - OpenRPC Interface Document
POST /agent/rpc - JSON-RPC Endpoint
Test:
curl -X POST http://localhost:8000/agent/rpc \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"add","params":{"a":10,"b":20},"id":1}'
"""
from fastapi import FastAPI
from anp.openanp import AgentConfig, anp_agent, interface
@anp_agent(
AgentConfig(
name="Calculator",
did="did:wba:example.com:calculator",
prefix="/agent",
description="A simple calculator agent",
)
)
class CalculatorAgent:
"""Minimal calculator agent."""
@interface
async def add(self, a: int, b: int) -> int:
"""Calculate the sum of two numbers.
Args:
a: First number
b: Second number
Returns:
Sum of the two numbers
"""
return a + b
@interface
async def multiply(self, a: int, b: int) -> int:
"""Calculate the product of two numbers.
Args:
a: First number
b: Second number
Returns:
Product of the two numbers
"""
return a * b
app = FastAPI(title="Calculator Agent")
app.include_router(CalculatorAgent.router())
if __name__ == "__main__":
import uvicorn
print("Starting Minimal ANP Server...")
print(" Agent Description: http://localhost:8000/agent/ad.json")
print(" OpenRPC Document: http://localhost:8000/agent/interface.json")
print(" JSON-RPC Endpoint: http://localhost:8000/agent/rpc")
uvicorn.run(app, host="0.0.0.0", port=8000)