Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/basic_memory/mcp/tools/write_note.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand All @@ -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,
Expand Down
14 changes: 12 additions & 2 deletions src/basic_memory/services/entity_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
184 changes: 183 additions & 1 deletion tests/mcp/test_tool_write_note.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -476,3 +476,185 @@ 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


@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