Repository navigation
feat(mcp): add bm_project, bm_project_delete and bm_schema #1839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d100dd0
feat(mcp): add bm_project and bm_schema, the grouped 1.0 tools
phernandez b0374d1
refactor(mcp): one shape for grouped tools; bm_project_delete as its …
phernandez d70a399
fix(mcp): gate legacy delete_project, refuse empty infer, honest rece…
phernandez aef642a
test(mcp): cover bm_project_delete JSON and infer with no notes
phernandez 9505abf
docs(mcp): present-tense module docs for bm_project and bm_schema
phernandez a416239
fix(cli): bm tool schema-* keep "nothing to report" as exit 0
phernandez 7a19a4a
fix(mcp): bm_project_delete refusals honor output_format
phernandez b064c44
fix(mcp): schema diff refuses a type with no notes; missing project i…
phernandez bcd188b
fix(mcp): bm_project recent has one JSON shape; API delete refusals k…
phernandez 492746d
fix(mcp): project deletion reports the backend's outcome in JSON
phernandez 45ccab0
fix(mcp): bm_schema refuses note_type together with identifier
phernandez 8800fe8
fix(mcp): workspace selection failures are refusals in the requested …
phernandez 26efe4a
fix(mcp): legacy delete_project renders API refusals in the requested…
phernandez 0df63af
refactor(mcp): one api_refusals block for project create and delete
phernandez 6d8729e
fix(mcp): bm_project recent JSON carries page, page_size and has_more
phernandez 8fd583a
fix(mcp): recent discovery keeps activity beside unindexed-project gu…
phernandez 731eae0
fix(mcp): schema tools render API refusals in the requested format
phernandez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| """bm_schema: validate notes against their schemas, infer schemas, find drift. | ||
|
|
||
| Each command runs the schema tool implementation of the same purpose; this tool | ||
| gives them one `command` parameter and the shared parameter types. | ||
| """ | ||
|
|
||
| from typing import Annotated, Any, Literal | ||
|
|
||
| from fastmcp import Context | ||
| from pydantic import AliasChoices, Field | ||
|
|
||
| from basic_memory.mcp.refusals import refusal | ||
| from basic_memory.mcp.server import mcp | ||
| from basic_memory.mcp.tool_params import ( | ||
| OutputFormat, | ||
| ProjectParam, | ||
| parse_project, | ||
| routing_arguments, | ||
| ) | ||
| from basic_memory.mcp.tools.schema import schema_diff, schema_infer, schema_validate | ||
| from basic_memory.schemas.schema import ValidationReport | ||
|
|
||
| type SchemaCommand = Literal["validate", "infer", "diff"] | ||
|
|
||
|
|
||
| @mcp.tool( | ||
| name="bm_schema", | ||
| title="Note schemas", | ||
| description=( | ||
| "Basic Memory note schemas (Picoschema). `command`: validate notes against their " | ||
| "schema (one `note_type`, one note by `identifier`, or every type that has a " | ||
| "schema); infer a schema from the notes of a `note_type`; diff a `note_type`'s " | ||
| "notes against its schema to find drift. A schema is a note with type: schema " | ||
| "whose `entity` field names the note type it describes." | ||
| ), | ||
| tags={"schema"}, | ||
| annotations={ | ||
| "title": "Note schemas", | ||
| "readOnlyHint": True, | ||
| "destructiveHint": False, | ||
| "idempotentHint": True, | ||
| "openWorldHint": False, | ||
| }, | ||
| ) | ||
| async def bm_schema( | ||
| command: Annotated[SchemaCommand, Field(description="What to do.")], | ||
| note_type: Annotated[ | ||
| str | None, | ||
| Field( | ||
| min_length=1, | ||
| validation_alias=AliasChoices("note_type", "type"), | ||
| description="The frontmatter `type:` to check. Required for infer and diff.", | ||
| ), | ||
| ] = None, | ||
| identifier: Annotated[ | ||
| str | None, | ||
| Field( | ||
| min_length=1, | ||
| validation_alias=AliasChoices("identifier", "file_path"), | ||
| description="For validate: one note, instead of a whole note type.", | ||
| ), | ||
| ] = None, | ||
| threshold: Annotated[ | ||
| float, | ||
| Field( | ||
| ge=0, | ||
| le=1, | ||
| description=("For infer: the share of notes a field must appear in to be suggested."), | ||
| ), | ||
| ] = 0.25, | ||
| output_format: OutputFormat = "text", | ||
| project: ProjectParam = None, | ||
| context: Context | None = None, | ||
| ) -> ValidationReport | str | dict[str, Any]: | ||
| """Validate, infer or diff note schemas.""" | ||
| # A note's own type decides its schema, so naming both would validate one and | ||
| # label the report as the other. | ||
| if note_type is not None and identifier is not None: | ||
| raise refusal( | ||
| "INVALID_ARGUMENTS", | ||
| "Give `note_type` to validate a type, or `identifier` to validate one note.", | ||
| output_format=output_format, | ||
| ) | ||
| project_name, project_id = routing_arguments(parse_project(project)) | ||
| if command == "validate": | ||
| return await schema_validate( | ||
| note_type=note_type, | ||
| identifier=identifier, | ||
|
phernandez marked this conversation as resolved.
Resolved
|
||
| project=project_name, | ||
| project_id=project_id, | ||
| output_format=output_format, | ||
| context=context, | ||
| ) | ||
| if identifier is not None: | ||
| raise refusal( | ||
| "INVALID_ARGUMENTS", | ||
| f"{command} works on a note type, not one note; give `note_type`.", | ||
| output_format=output_format, | ||
| ) | ||
| if note_type is None: | ||
| raise refusal( | ||
| "INVALID_ARGUMENTS", | ||
| f"{command} needs `note_type`, the frontmatter type to check.", | ||
| try_instead="bm_find(filters={'type': ...}) to see which types exist", | ||
| output_format=output_format, | ||
| ) | ||
| if command == "infer": | ||
| return await schema_infer( | ||
| note_type=note_type, | ||
| threshold=threshold, | ||
|
phernandez marked this conversation as resolved.
Resolved
|
||
| project=project_name, | ||
| project_id=project_id, | ||
| output_format=output_format, | ||
| context=context, | ||
| ) | ||
| return await schema_diff( | ||
| note_type=note_type, | ||
| project=project_name, | ||
| project_id=project_id, | ||
| output_format=output_format, | ||
| context=context, | ||
| ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.