agent-core-notify 0.8.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""agent_core_notify — Desktop notification MCP server."""
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
"""Desktop notification MCP server.
|
|
2
|
+
|
|
3
|
+
Provides toast notification tools that agents can call during a session.
|
|
4
|
+
Stays alive for the session lifetime, enabling interactive notifications
|
|
5
|
+
with reply fields and action buttons.
|
|
6
|
+
|
|
7
|
+
Tools:
|
|
8
|
+
send_notification: Fire-and-forget toast notification.
|
|
9
|
+
ask_user: Send notification with reply field, return immediately with ID.
|
|
10
|
+
notify_with_buttons: Send notification with action buttons, return immediately with ID.
|
|
11
|
+
get_reply: Check if the user has replied to a notification.
|
|
12
|
+
clear_notifications: Clear all active notifications.
|
|
13
|
+
|
|
14
|
+
Launch:
|
|
15
|
+
python -m agent_core_notify.mcp_server # stdio transport (for .mcp.json)
|
|
16
|
+
|
|
17
|
+
Register in .mcp.json:
|
|
18
|
+
{
|
|
19
|
+
"mcpServers": {
|
|
20
|
+
"notify": {
|
|
21
|
+
"command": "uv",
|
|
22
|
+
"args": [
|
|
23
|
+
"run",
|
|
24
|
+
"--no-sync",
|
|
25
|
+
"--directory",
|
|
26
|
+
"E:/workspaces/ai/agents/agent_core",
|
|
27
|
+
"python",
|
|
28
|
+
"-m",
|
|
29
|
+
"agent_core_notify.mcp_server"
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
from __future__ import annotations
|
|
37
|
+
|
|
38
|
+
import asyncio
|
|
39
|
+
import logging
|
|
40
|
+
import sys
|
|
41
|
+
import uuid
|
|
42
|
+
from pathlib import Path
|
|
43
|
+
|
|
44
|
+
from mcp.server.fastmcp import FastMCP
|
|
45
|
+
|
|
46
|
+
from desktop_notifier import (
|
|
47
|
+
Attachment,
|
|
48
|
+
Button,
|
|
49
|
+
DesktopNotifier,
|
|
50
|
+
Icon,
|
|
51
|
+
ReplyField,
|
|
52
|
+
Sound,
|
|
53
|
+
Urgency,
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
# Logging to stderr — stdout is reserved for MCP stdio transport
|
|
57
|
+
logging.basicConfig(
|
|
58
|
+
stream=sys.stderr,
|
|
59
|
+
level=logging.INFO,
|
|
60
|
+
format="%(asctime)s %(levelname)s %(message)s",
|
|
61
|
+
datefmt="%H:%M:%S",
|
|
62
|
+
)
|
|
63
|
+
logger = logging.getLogger("agent_core_notify")
|
|
64
|
+
|
|
65
|
+
mcp = FastMCP("agent-core-notify")
|
|
66
|
+
|
|
67
|
+
# Shared notifier instance
|
|
68
|
+
_notifier = DesktopNotifier(app_name="Agent Core")
|
|
69
|
+
|
|
70
|
+
# Store for pending interactive notifications (id -> reply/button result)
|
|
71
|
+
_pending: dict[str, dict] = {}
|
|
72
|
+
# Reference to the running event loop for thread-safe callback signaling
|
|
73
|
+
_loop: asyncio.AbstractEventLoop | None = None
|
|
74
|
+
|
|
75
|
+
URGENCY_MAP = {
|
|
76
|
+
"low": Urgency.Low,
|
|
77
|
+
"normal": Urgency.Normal,
|
|
78
|
+
"critical": Urgency.Critical,
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _make_sound(sound: str | None) -> Sound | None:
|
|
83
|
+
"""Create a Sound object — auto-detects file path vs system sound name."""
|
|
84
|
+
if not sound:
|
|
85
|
+
return None
|
|
86
|
+
if Path(sound).suffix in (".wav", ".mp3", ".ogg", ".flac", ".aac", ".wma"):
|
|
87
|
+
return Sound(path=Path(sound))
|
|
88
|
+
return Sound(name=sound)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _resolve_reply(notification_id: str, reply: str) -> None:
|
|
92
|
+
"""Thread-safe callback for reply fields."""
|
|
93
|
+
if notification_id in _pending:
|
|
94
|
+
_pending[notification_id]["reply"] = reply
|
|
95
|
+
_pending[notification_id]["status"] = "replied"
|
|
96
|
+
logger.info("Reply received for %s: %s", notification_id, reply)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _resolve_button(notification_id: str, button: str) -> None:
|
|
100
|
+
"""Thread-safe callback for button presses."""
|
|
101
|
+
if notification_id in _pending:
|
|
102
|
+
_pending[notification_id]["reply"] = button
|
|
103
|
+
_pending[notification_id]["status"] = "clicked"
|
|
104
|
+
logger.info("Button clicked for %s: %s", notification_id, button)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _resolve_dismissed(notification_id: str) -> None:
|
|
108
|
+
"""Thread-safe callback for dismissed notifications."""
|
|
109
|
+
if notification_id in _pending:
|
|
110
|
+
_pending[notification_id]["reply"] = ""
|
|
111
|
+
_pending[notification_id]["status"] = "dismissed"
|
|
112
|
+
logger.info("Notification dismissed: %s", notification_id)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
@mcp.tool()
|
|
116
|
+
async def send_notification(
|
|
117
|
+
title: str,
|
|
118
|
+
message: str,
|
|
119
|
+
urgency: str = "normal",
|
|
120
|
+
icon: str | None = None,
|
|
121
|
+
attachment: str | None = None,
|
|
122
|
+
sound: str | None = None,
|
|
123
|
+
thread: str | None = None,
|
|
124
|
+
) -> dict[str, str]:
|
|
125
|
+
"""Send a desktop toast notification (fire-and-forget).
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
title: Notification title (e.g., agent name).
|
|
129
|
+
message: Notification body text.
|
|
130
|
+
urgency: "low", "normal", or "critical". Default: "normal".
|
|
131
|
+
icon: Path to an icon image file. Optional.
|
|
132
|
+
attachment: Path to a file to attach. Optional.
|
|
133
|
+
sound: Name of a system sound, or path to a sound file. Optional.
|
|
134
|
+
thread: Thread ID to group related notifications. Optional.
|
|
135
|
+
"""
|
|
136
|
+
urg = URGENCY_MAP.get(urgency, Urgency.Normal)
|
|
137
|
+
icon_obj = Icon(path=Path(icon)) if icon else None
|
|
138
|
+
attach_obj = Attachment(path=Path(attachment)) if attachment else None
|
|
139
|
+
sound_obj = _make_sound(sound)
|
|
140
|
+
|
|
141
|
+
await _notifier.send(
|
|
142
|
+
title=title,
|
|
143
|
+
message=message,
|
|
144
|
+
urgency=urg,
|
|
145
|
+
icon=icon_obj,
|
|
146
|
+
attachment=attach_obj,
|
|
147
|
+
sound=sound_obj,
|
|
148
|
+
thread=thread,
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
logger.info("Notification sent: %s — %s", title, message)
|
|
152
|
+
return {"status": "sent", "title": title, "message": message}
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
@mcp.tool()
|
|
156
|
+
async def ask_user(
|
|
157
|
+
title: str,
|
|
158
|
+
message: str,
|
|
159
|
+
reply_button_title: str = "Reply",
|
|
160
|
+
urgency: str = "normal",
|
|
161
|
+
icon: str | None = None,
|
|
162
|
+
sound: str | None = None,
|
|
163
|
+
thread: str | None = None,
|
|
164
|
+
) -> dict[str, str]:
|
|
165
|
+
"""Send a notification with a reply field. Returns immediately with a notification_id.
|
|
166
|
+
|
|
167
|
+
Use get_reply(notification_id) to check if the user has replied.
|
|
168
|
+
This does NOT block — the agent can continue working while waiting.
|
|
169
|
+
|
|
170
|
+
Args:
|
|
171
|
+
title: Notification title.
|
|
172
|
+
message: The question or prompt to show.
|
|
173
|
+
reply_button_title: Label for the reply button. Default: "Reply".
|
|
174
|
+
urgency: "low", "normal", or "critical". Default: "normal".
|
|
175
|
+
icon: Path to an icon image file. Optional.
|
|
176
|
+
sound: Name of a system sound, or path to a sound file. Optional.
|
|
177
|
+
thread: Thread ID to group related notifications. Optional.
|
|
178
|
+
"""
|
|
179
|
+
notification_id = str(uuid.uuid4())[:8]
|
|
180
|
+
_pending[notification_id] = {"status": "pending", "reply": ""}
|
|
181
|
+
|
|
182
|
+
urg = URGENCY_MAP.get(urgency, Urgency.Normal)
|
|
183
|
+
icon_obj = Icon(path=Path(icon)) if icon else None
|
|
184
|
+
sound_obj = _make_sound(sound)
|
|
185
|
+
|
|
186
|
+
await _notifier.send(
|
|
187
|
+
title=title,
|
|
188
|
+
message=message,
|
|
189
|
+
urgency=urg,
|
|
190
|
+
icon=icon_obj,
|
|
191
|
+
sound=sound_obj,
|
|
192
|
+
thread=thread,
|
|
193
|
+
reply_field=ReplyField(
|
|
194
|
+
title="Type your reply",
|
|
195
|
+
button_title=reply_button_title,
|
|
196
|
+
on_replied=lambda text: _resolve_reply(notification_id, text),
|
|
197
|
+
),
|
|
198
|
+
on_dismissed=lambda: _resolve_dismissed(notification_id),
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
logger.info("Ask sent [%s]: %s — %s", notification_id, title, message)
|
|
202
|
+
return {"status": "pending", "notification_id": notification_id}
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
@mcp.tool()
|
|
206
|
+
async def notify_with_buttons(
|
|
207
|
+
title: str,
|
|
208
|
+
message: str,
|
|
209
|
+
buttons: list[str],
|
|
210
|
+
urgency: str = "normal",
|
|
211
|
+
icon: str | None = None,
|
|
212
|
+
sound: str | None = None,
|
|
213
|
+
thread: str | None = None,
|
|
214
|
+
) -> dict[str, str]:
|
|
215
|
+
"""Send a notification with action buttons. Returns immediately with a notification_id.
|
|
216
|
+
|
|
217
|
+
Use get_reply(notification_id) to check which button the user clicked.
|
|
218
|
+
This does NOT block — the agent can continue working while waiting.
|
|
219
|
+
|
|
220
|
+
Args:
|
|
221
|
+
title: Notification title.
|
|
222
|
+
message: Notification body text.
|
|
223
|
+
buttons: List of button labels (e.g., ["Approve", "Deny", "Snooze"]).
|
|
224
|
+
urgency: "low", "normal", or "critical". Default: "normal".
|
|
225
|
+
icon: Path to an icon image file. Optional.
|
|
226
|
+
sound: Name of a system sound, or path to a sound file. Optional.
|
|
227
|
+
thread: Thread ID to group related notifications. Optional.
|
|
228
|
+
"""
|
|
229
|
+
notification_id = str(uuid.uuid4())[:8]
|
|
230
|
+
_pending[notification_id] = {"status": "pending", "reply": ""}
|
|
231
|
+
|
|
232
|
+
button_objs = [
|
|
233
|
+
Button(
|
|
234
|
+
title=label,
|
|
235
|
+
on_pressed=lambda lbl=label: _resolve_button(notification_id, lbl),
|
|
236
|
+
)
|
|
237
|
+
for label in buttons
|
|
238
|
+
]
|
|
239
|
+
|
|
240
|
+
urg = URGENCY_MAP.get(urgency, Urgency.Normal)
|
|
241
|
+
icon_obj = Icon(path=Path(icon)) if icon else None
|
|
242
|
+
sound_obj = _make_sound(sound)
|
|
243
|
+
|
|
244
|
+
await _notifier.send(
|
|
245
|
+
title=title,
|
|
246
|
+
message=message,
|
|
247
|
+
urgency=urg,
|
|
248
|
+
icon=icon_obj,
|
|
249
|
+
sound=sound_obj,
|
|
250
|
+
thread=thread,
|
|
251
|
+
buttons=button_objs,
|
|
252
|
+
on_dismissed=lambda: _resolve_dismissed(notification_id),
|
|
253
|
+
)
|
|
254
|
+
|
|
255
|
+
logger.info("Buttons sent [%s]: %s — %s [%s]", notification_id, title, message, ", ".join(buttons))
|
|
256
|
+
return {"status": "pending", "notification_id": notification_id}
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
@mcp.tool()
|
|
260
|
+
async def get_reply(notification_id: str) -> dict[str, str]:
|
|
261
|
+
"""Check if the user has replied to or interacted with a notification.
|
|
262
|
+
|
|
263
|
+
Returns the current status:
|
|
264
|
+
- {"status": "pending"} — user hasn't responded yet
|
|
265
|
+
- {"status": "replied", "reply": "user's text"} — user typed a reply
|
|
266
|
+
- {"status": "clicked", "button": "Approve"} — user clicked a button
|
|
267
|
+
- {"status": "dismissed"} — user dismissed the notification
|
|
268
|
+
- {"status": "not_found"} — unknown notification_id
|
|
269
|
+
|
|
270
|
+
Args:
|
|
271
|
+
notification_id: The ID returned by ask_user or notify_with_buttons.
|
|
272
|
+
"""
|
|
273
|
+
if notification_id not in _pending:
|
|
274
|
+
return {"status": "not_found", "notification_id": notification_id}
|
|
275
|
+
|
|
276
|
+
entry = _pending[notification_id]
|
|
277
|
+
result = {"status": entry["status"], "notification_id": notification_id}
|
|
278
|
+
|
|
279
|
+
if entry["status"] == "replied":
|
|
280
|
+
result["reply"] = entry["reply"]
|
|
281
|
+
del _pending[notification_id]
|
|
282
|
+
elif entry["status"] == "clicked":
|
|
283
|
+
result["button"] = entry["reply"]
|
|
284
|
+
del _pending[notification_id]
|
|
285
|
+
elif entry["status"] == "dismissed":
|
|
286
|
+
del _pending[notification_id]
|
|
287
|
+
|
|
288
|
+
return result
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
@mcp.tool()
|
|
292
|
+
async def clear_notifications() -> dict[str, str]:
|
|
293
|
+
"""Clear all active notifications sent by this server."""
|
|
294
|
+
await _notifier.clear_all()
|
|
295
|
+
_pending.clear()
|
|
296
|
+
logger.info("All notifications cleared")
|
|
297
|
+
return {"status": "cleared"}
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
def run() -> None:
|
|
301
|
+
"""Launch the MCP server on stdio transport."""
|
|
302
|
+
try:
|
|
303
|
+
mcp.run(transport="stdio")
|
|
304
|
+
finally:
|
|
305
|
+
# desktop-notifier's WinRT backend keeps non-daemon threads alive,
|
|
306
|
+
# which blocks normal interpreter shutdown on Windows. Force-exit
|
|
307
|
+
# so the agent-core-notify entry-point .exe isn't held open after
|
|
308
|
+
# stdio closes (otherwise `uv sync` can't replace it on the next run).
|
|
309
|
+
import os
|
|
310
|
+
os._exit(0)
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
if __name__ == "__main__":
|
|
314
|
+
run()
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
agent_core_notify/__init__.py,sha256=KOVY-JMEoGbGwoII6JK3N8hDUxphnOKniz6WSyAklo4,61
|
|
2
|
+
agent_core_notify/mcp_server.py,sha256=fKkRl6O04g0hUTOC6qx4S9UiANPYMPm-IeY6f4W6pIo,10220
|
|
3
|
+
agent_core_notify-0.8.2.dist-info/METADATA,sha256=0L2SPj-_dLGd9Mzd6ThGKV8G8ULOepvoXiuNHFHBuE8,235
|
|
4
|
+
agent_core_notify-0.8.2.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
|
|
5
|
+
agent_core_notify-0.8.2.dist-info/entry_points.txt,sha256=re9xh8RJY2YsZyGlpbkxGjSxtZ8InMnFrzUpeqTefvA,71
|
|
6
|
+
agent_core_notify-0.8.2.dist-info/RECORD,,
|