From b386c5453bfb4b8f814fc33b332052ab423c09cd Mon Sep 17 00:00:00 2001 From: "claude[bot]" <209825114+claude[bot]@users.noreply.github.com> Date: Wed, 18 Jun 2025 14:14:12 +0000 Subject: [PATCH 1/4] fix: add entity_type parameter to write_note MCP tool - Add optional entity_type parameter with default "note" - Update function signature and docstring - Replace hardcoded entity_type="note" with parameter - Add comprehensive tests for different entity types - Ensure backward compatibility with default behavior Fixes #144 Co-authored-by: Paul Hernandez --- src/basic_memory/mcp/tools/write_note.py | 4 +- tests/mcp/test_tool_write_note.py | 144 +++++++++++++++++++++++ 2 files changed, 147 insertions(+), 1 deletion(-) diff --git a/src/basic_memory/mcp/tools/write_note.py b/src/basic_memory/mcp/tools/write_note.py index 70ce2947a..2ba0353c8 100644 --- a/src/basic_memory/mcp/tools/write_note.py +++ b/src/basic_memory/mcp/tools/write_note.py @@ -27,6 +27,7 @@ async def write_note( content: str, folder: str, tags=None, # Remove type hint completely to avoid schema issues + entity_type: str = "note", project: Optional[str] = None, ) -> str: """Write a markdown note to the knowledge base. @@ -58,6 +59,7 @@ async def write_note( Use forward slashes (/) as separators. Examples: "notes", "projects/2025", "research/ml" tags: Tags to categorize the note. Can be a list of strings, a comma-separated string, or None. Note: If passing from external MCP clients, use a string format (e.g. "tag1,tag2,tag3") + entity_type: Type of entity to create. Defaults to "note". Can be "guide", "report", "config", etc. project: Optional project name to write to. If not provided, uses current active project. Returns: @@ -84,7 +86,7 @@ async def write_note( entity = Entity( title=title, folder=folder, - entity_type="note", + entity_type=entity_type, content_type="text/markdown", content=content, entity_metadata=metadata, diff --git a/tests/mcp/test_tool_write_note.py b/tests/mcp/test_tool_write_note.py index f5cea1b7a..7f61f1b8b 100644 --- a/tests/mcp/test_tool_write_note.py +++ b/tests/mcp/test_tool_write_note.py @@ -476,3 +476,147 @@ async def test_write_note_permalink_collision_fix_issue_139(app): # Original note should still exist original_content = await read_note.fn("test/note-1") assert "Original content for note 1" in original_content + + +@pytest.mark.asyncio +async def test_write_note_with_custom_entity_type(app): + """Test creating a note with custom entity_type parameter. + + This test verifies the fix for Issue #144 where entity_type parameter + was hardcoded to "note" instead of allowing custom types. + """ + result = await write_note.fn( + title="Test Guide", + folder="guides", + content="# Guide Content\nThis is a guide", + tags=["guide", "documentation"], + entity_type="guide", + ) + + assert result + assert "# Created note" in result + assert "file_path: guides/Test Guide.md" in result + assert "permalink: guides/test-guide" in result + assert "## Tags" in result + assert "- guide, documentation" in result + + # Verify the entity type is correctly set in the frontmatter + content = await read_note.fn("guides/test-guide") + assert ( + dedent(""" + --- + title: Test Guide + type: guide + permalink: guides/test-guide + tags: + - guide + - documentation + --- + + # Guide Content + This is a guide + """).strip() + in content + ) + + +@pytest.mark.asyncio +async def test_write_note_with_report_entity_type(app): + """Test creating a note with entity_type="report".""" + result = await write_note.fn( + title="Monthly Report", + folder="reports", + content="# Monthly Report\nThis is a monthly report", + tags=["report", "monthly"], + entity_type="report", + ) + + assert result + assert "# Created note" in result + assert "file_path: reports/Monthly Report.md" in result + assert "permalink: reports/monthly-report" in result + + # Verify the entity type is correctly set in the frontmatter + content = await read_note.fn("reports/monthly-report") + assert "type: report" in content + assert "# Monthly Report" in content + + +@pytest.mark.asyncio +async def test_write_note_with_config_entity_type(app): + """Test creating a note with entity_type="config".""" + result = await write_note.fn( + title="System Config", + folder="config", + content="# System Configuration\nThis is a config file", + entity_type="config", + ) + + assert result + assert "# Created note" in result + assert "file_path: config/System Config.md" in result + assert "permalink: config/system-config" in result + + # Verify the entity type is correctly set in the frontmatter + content = await read_note.fn("config/system-config") + assert "type: config" in content + assert "# System Configuration" in content + + +@pytest.mark.asyncio +async def test_write_note_entity_type_default_behavior(app): + """Test that the entity_type parameter defaults to "note" when not specified. + + This ensures backward compatibility - existing code that doesn't specify + entity_type should continue to work as before. + """ + result = await write_note.fn( + title="Default Type Test", + folder="test", + content="# Default Type Test\nThis should be type 'note'", + tags=["test"], + ) + + assert result + assert "# Created note" in result + assert "file_path: test/Default Type Test.md" in result + assert "permalink: test/default-type-test" in result + + # Verify the entity type defaults to "note" + content = await read_note.fn("test/default-type-test") + assert "type: note" in content + assert "# Default Type Test" in content + + +@pytest.mark.asyncio +async def test_write_note_update_existing_with_different_entity_type(app): + """Test updating an existing note with a different entity_type.""" + # Create initial note as "note" type + result1 = await write_note.fn( + title="Changeable Type", + folder="test", + content="# Initial Content\nThis starts as a note", + tags=["test"], + entity_type="note", + ) + + assert result1 + assert "# Created note" in result1 + + # Update the same note with a different entity_type + result2 = await write_note.fn( + title="Changeable Type", + folder="test", + content="# Updated Content\nThis is now a guide", + tags=["guide"], + entity_type="guide", + ) + + assert result2 + assert "# Updated note" in result2 + + # Verify the entity type was updated + content = await read_note.fn("test/changeable-type") + assert "type: guide" in content + assert "# Updated Content" in content + assert "- guide" in content From ab7da9c6f895a7ee81795a1d7af5a7929087c647 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <209825114+claude[bot]@users.noreply.github.com> Date: Wed, 18 Jun 2025 14:35:46 +0000 Subject: [PATCH 2/4] test: add test for frontmatter entity_type handling Add test_write_note_respects_frontmatter_entity_type to verify that when write_note is called without entity_type parameter, but the content includes frontmatter with a 'type' field, that type is respected instead of defaulting to 'note'. Co-authored-by: Paul Hernandez --- tests/mcp/test_tool_write_note.py | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/mcp/test_tool_write_note.py b/tests/mcp/test_tool_write_note.py index 7f61f1b8b..2eae56ced 100644 --- a/tests/mcp/test_tool_write_note.py +++ b/tests/mcp/test_tool_write_note.py @@ -620,3 +620,45 @@ async def test_write_note_update_existing_with_different_entity_type(app): assert "type: guide" in content assert "# Updated Content" in content assert "- guide" in content + + +@pytest.mark.asyncio +async def test_write_note_respects_frontmatter_entity_type(app): + """Test that entity_type in frontmatter is respected when parameter is not provided. + + This verifies that when write_note is called without entity_type parameter, + but the content includes frontmatter with a 'type' field, that type is respected + instead of defaulting to 'note'. + """ + note = dedent(""" + --- + title: Test Guide + type: guide + permalink: guides/test-guide + tags: + - guide + - documentation + --- + + # Guide Content + This is a guide + """).strip() + + # Call write_note without entity_type parameter - it should respect frontmatter type + result = await write_note.fn( + title="Test Guide", + folder="guides", + content=note + ) + + assert result + assert "# Created note" in result + assert "file_path: guides/Test Guide.md" in result + assert "permalink: guides/test-guide" in result + + # Verify the entity type from frontmatter is respected (should be "guide", not "note") + content = await read_note.fn("guides/test-guide") + assert "type: guide" in content + assert "# Guide Content" in content + assert "- guide" in content + assert "- documentation" in content From ed08df6abc9d96f273006298218cdbb99407941b Mon Sep 17 00:00:00 2001 From: "claude[bot]" <209825114+claude[bot]@users.noreply.github.com> Date: Wed, 18 Jun 2025 14:47:34 +0000 Subject: [PATCH 3/4] fix: correct frontmatter delimiter in test_write_note_no_tags test The test had a malformed YAML frontmatter delimiter (-- instead of ---) which was causing parsing failures. This fixes the test to use proper YAML frontmatter formatting. Co-authored-by: Paul Hernandez --- tests/mcp/test_tool_write_note.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/mcp/test_tool_write_note.py b/tests/mcp/test_tool_write_note.py index 2eae56ced..f5daa4006 100644 --- a/tests/mcp/test_tool_write_note.py +++ b/tests/mcp/test_tool_write_note.py @@ -63,7 +63,7 @@ async def test_write_note_no_tags(app): content = await read_note.fn("test/simple-note") assert ( dedent(""" - -- + --- title: Simple Note type: note permalink: test/simple-note From 876fc7000bb729f39eb6f31e342513e9d74a288b Mon Sep 17 00:00:00 2001 From: phernandez Date: Wed, 18 Jun 2025 16:37:02 -0500 Subject: [PATCH 4/4] fix: respect frontmatter entity_type in write_note operations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add frontmatter type extraction in EntityService.create_entity() and update_entity() - Override schema.entity_type when content frontmatter contains 'type' field - Fix lint error in alembic/env.py with noqa comment - Update test formatting for consistency - Ensures write_note respects both parameter and frontmatter entity_type values Fixes #144 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/basic_memory/services/entity_service.py | 14 ++++++++++++-- tests/mcp/test_tool_write_note.py | 10 +++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/basic_memory/services/entity_service.py b/src/basic_memory/services/entity_service.py index 535382d5b..57cc6cec2 100644 --- a/src/basic_memory/services/entity_service.py +++ b/src/basic_memory/services/entity_service.py @@ -117,10 +117,15 @@ async def create_entity(self, schema: EntitySchema) -> EntityModel: f"file for entity {schema.folder}/{schema.title} already exists: {file_path}" ) - # Parse content frontmatter to check for user-specified permalink + # Parse content frontmatter to check for user-specified permalink and entity_type content_markdown = None if schema.content and has_frontmatter(schema.content): content_frontmatter = parse_frontmatter(schema.content) + + # If content has entity_type/type, use it to override the schema entity_type + if "type" in content_frontmatter: + schema.entity_type = content_frontmatter["type"] + if "permalink" in content_frontmatter: # Create a minimal EntityMarkdown object for permalink resolution from basic_memory.markdown.schemas import EntityFrontmatter @@ -172,10 +177,15 @@ async def update_entity(self, entity: EntityModel, schema: EntitySchema) -> Enti # Read existing frontmatter from the file if it exists existing_markdown = await self.entity_parser.parse_file(file_path) - # Parse content frontmatter to check for user-specified permalink + # Parse content frontmatter to check for user-specified permalink and entity_type content_markdown = None if schema.content and has_frontmatter(schema.content): content_frontmatter = parse_frontmatter(schema.content) + + # If content has entity_type/type, use it to override the schema entity_type + if "type" in content_frontmatter: + schema.entity_type = content_frontmatter["type"] + if "permalink" in content_frontmatter: # Create a minimal EntityMarkdown object for permalink resolution from basic_memory.markdown.schemas import EntityFrontmatter diff --git a/tests/mcp/test_tool_write_note.py b/tests/mcp/test_tool_write_note.py index f5daa4006..fe9970a88 100644 --- a/tests/mcp/test_tool_write_note.py +++ b/tests/mcp/test_tool_write_note.py @@ -622,10 +622,10 @@ async def test_write_note_update_existing_with_different_entity_type(app): assert "- guide" in content -@pytest.mark.asyncio +@pytest.mark.asyncio async def test_write_note_respects_frontmatter_entity_type(app): """Test that entity_type in frontmatter is respected when parameter is not provided. - + This verifies that when write_note is called without entity_type parameter, but the content includes frontmatter with a 'type' field, that type is respected instead of defaulting to 'note'. @@ -645,11 +645,7 @@ async def test_write_note_respects_frontmatter_entity_type(app): """).strip() # Call write_note without entity_type parameter - it should respect frontmatter type - result = await write_note.fn( - title="Test Guide", - folder="guides", - content=note - ) + result = await write_note.fn(title="Test Guide", folder="guides", content=note) assert result assert "# Created note" in result