Skip to content

Commit 66f9236

Browse files
authored
Merge pull request #16 from nmeierpolys/main
Add a replace_note method using the add-text endpoint
2 parents 6d8c11a + 537cc03 commit 66f9236

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Refer to Bear's [X-callback-url Scheme documentation](https://bear.app/faq/x-cal
7878

7979
- [x] /open-note
8080
- [x] /create
81-
- [ ] /add-text
81+
- [x] /add-text (partially, via the replace_note method)
8282
- [ ] /add-file
8383
- [x] /tags
8484
- [x] /open-tag

src/mcp_bear/__init__.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def error(request: Request) -> None:
7474
class AppContext:
7575
open_note_results: Queue[Future[QueryParams]]
7676
create_results: Queue[Future[QueryParams]]
77+
add_text_results: Queue[Future[QueryParams]]
7778
tags_results: Queue[Future[QueryParams]]
7879
open_tag_results: Queue[Future[QueryParams]]
7980
todo_results: Queue[Future[QueryParams]]
@@ -103,6 +104,7 @@ async def app_lifespan(_server: FastMCP, uds: Path) -> AsyncIterator[AppContext]
103104
yield AppContext(
104105
open_note_results=register_callback(callback, "open-note"),
105106
create_results=register_callback(callback, "create"),
107+
add_text_results=register_callback(callback, "add-text"),
106108
tags_results=register_callback(callback, "tags"),
107109
open_tag_results=register_callback(callback, "open-tag"),
108110
todo_results=register_callback(callback, "todo"),
@@ -186,6 +188,47 @@ async def create(
186188

187189
return res.get("identifier") or ""
188190

191+
@mcp.tool()
192+
async def replace_note(
193+
ctx: Context,
194+
id: str | None = Field(description="note unique identifier", default=None),
195+
title: str | None = Field(description="new title for the note", default=None),
196+
text: str | None = Field(description="new text to replace note content", default=None),
197+
tags: list[str] | None = Field(description="list of tags to add to the note", default=None),
198+
timestamp: bool = Field(description="prepend the current date and time to the text", default=False),
199+
) -> str:
200+
"""Replace the content of an existing note identified by its id."""
201+
app_ctx: AppContext = ctx.request_context.lifespan_context # type: ignore
202+
future = Future[QueryParams]()
203+
await app_ctx.add_text_results.put(future)
204+
205+
mode = "replace_all" if title is not None else "replace"
206+
207+
params = {
208+
"mode": mode,
209+
"open_note": "no",
210+
"new_window": "no",
211+
"show_window": "no",
212+
"edit": "no",
213+
"x-success": f"xfwder://{uds.stem}/add-text/success",
214+
"x-error": f"xfwder://{uds.stem}/add-text/error",
215+
}
216+
if id is not None:
217+
params["id"] = id
218+
if text is not None:
219+
params["text"] = text
220+
if title is not None:
221+
params["title"] = title
222+
if tags is not None:
223+
params["tags"] = ",".join(tags)
224+
if timestamp:
225+
params["timestamp"] = "yes"
226+
227+
webbrowser.open(f"{BASE_URL}/add-text?{urlencode(params, quote_via=quote)}")
228+
res = await future
229+
230+
return unquote_plus(res.get("note") or "")
231+
189232
@mcp.tool()
190233
async def tags(
191234
ctx: Context,

tests/test_server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ async def test_list_tools(mcp_client_session: ClientSession) -> None:
4040
assert "today" in tools
4141
assert "search" in tools
4242
assert "grab_url" in tools
43+
assert "replace_note" in tools

0 commit comments

Comments
 (0)