This repository was archived by the owner on Feb 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
180 lines (139 loc) · 5.3 KB
/
server.py
File metadata and controls
180 lines (139 loc) · 5.3 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""MCP server exposing keep-protocol tools.
Run with: python -m keep.mcp
Or after install: keep-mcp
"""
import json
import os
from typing import Optional
from mcp.server.fastmcp import FastMCP
from keep.client import KeepClient
# Configuration from environment
KEEP_HOST = os.environ.get("KEEP_HOST", "localhost")
KEEP_PORT = int(os.environ.get("KEEP_PORT", "9009"))
KEEP_SRC = os.environ.get("KEEP_SRC", "bot:mcp-agent")
# Create MCP server
mcp = FastMCP("keep-protocol")
def _get_client() -> KeepClient:
"""Create a KeepClient with configured host/port/src."""
return KeepClient(host=KEEP_HOST, port=KEEP_PORT, src=KEEP_SRC)
@mcp.tool()
def keep_send(
dst: str,
body: str,
fee: int = 0,
ttl: int = 60,
scar: str = "",
) -> str:
"""Send a signed packet to another AI agent via keep-protocol.
Uses ed25519 signatures over TCP+Protobuf for authenticated,
low-latency agent-to-agent communication.
Args:
dst: Destination agent or routing target (e.g., 'bot:weather', 'bot:planner', 'server')
body: The message or intent to send
fee: Micro-fee in sats for anti-spam (default: 0)
ttl: Time-to-live in seconds (default: 60)
scar: Optional scar/memory data to share (as string)
Returns:
Response body from the destination, or "done" if server acknowledged.
"""
client = _get_client()
scar_bytes = scar.encode("utf-8") if scar else b""
try:
reply = client.send(
body=body,
dst=dst,
fee=fee,
ttl=ttl,
scar=scar_bytes,
)
return reply.body if reply else "sent"
except ConnectionRefusedError:
return f"error: keep-server not running on {KEEP_HOST}:{KEEP_PORT}"
except Exception as e:
return f"error: {str(e)}"
@mcp.tool()
def keep_discover(query: str = "info") -> str:
"""Discover keep-protocol server info and connected agents.
Args:
query: Discovery type - "info" for server version/uptime,
"agents" for connected agent list, "stats" for scar exchange metrics.
Returns:
JSON string with discovery results.
"""
client = _get_client()
try:
result = client.discover(query)
return json.dumps(result, indent=2)
except ConnectionRefusedError:
return json.dumps({"error": f"keep-server not running on {KEEP_HOST}:{KEEP_PORT}"})
except Exception as e:
return json.dumps({"error": str(e)})
@mcp.tool()
def keep_discover_agents() -> str:
"""List all currently connected agent identities.
Returns:
JSON array of agent identity strings (e.g., ["bot:alice", "bot:weather"]).
"""
client = _get_client()
try:
agents = client.discover_agents()
return json.dumps(agents)
except ConnectionRefusedError:
return json.dumps({"error": f"keep-server not running on {KEEP_HOST}:{KEEP_PORT}"})
except Exception as e:
return json.dumps({"error": str(e)})
@mcp.tool()
def keep_listen(timeout: int = 10, register_src: Optional[str] = None) -> str:
"""Register this agent and listen for incoming messages.
Opens a persistent connection, registers the agent identity, and listens
for packets from other agents for the specified duration.
Args:
timeout: Seconds to listen before returning (default: 10)
register_src: Optional custom identity to register (uses KEEP_SRC env if not set)
Returns:
JSON object with received messages: {"messages": [...], "count": N}
"""
src = register_src or KEEP_SRC
messages = []
def on_message(packet):
messages.append({
"src": packet.src,
"dst": packet.dst,
"body": packet.body,
})
try:
with KeepClient(host=KEEP_HOST, port=KEEP_PORT, src=src) as client:
# Register with the server
client.send(body="register", dst="server", wait_reply=True)
# Listen for incoming packets
client.listen(on_message, timeout=timeout)
return json.dumps({"messages": messages, "count": len(messages)})
except ConnectionRefusedError:
return json.dumps({"error": f"keep-server not running on {KEEP_HOST}:{KEEP_PORT}"})
except Exception as e:
return json.dumps({"error": str(e)})
@mcp.tool()
def keep_ensure_server() -> str:
"""Ensure a keep-protocol server is running, starting one if needed.
Checks if the server is reachable. If not, attempts to start one using:
1. Docker (preferred): pulls and runs the multi-arch image
2. Go fallback: installs and runs via `go install`
Returns:
JSON object: {"running": true/false, "method": "existing"|"docker"|"go"|"failed"}
"""
# Check if already running
if KeepClient._is_port_open(KEEP_HOST, KEEP_PORT):
return json.dumps({"running": True, "method": "existing"})
# Try to start
success = KeepClient.ensure_server(host=KEEP_HOST, port=KEEP_PORT)
if success:
# Determine which method worked
method = "docker" if KeepClient._has_docker() else "go"
return json.dumps({"running": True, "method": method})
else:
return json.dumps({"running": False, "method": "failed"})
def main():
"""Entry point for keep-mcp command."""
mcp.run(transport="stdio")
if __name__ == "__main__":
main()