-
Notifications
You must be signed in to change notification settings - Fork 3
Prompt: Enable user-defined instructions #57
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,77 +1,4 @@ | ||
| import importlib.resources | ||
| from cratedb_mcp.core import CrateDbMcp | ||
|
|
||
| from cratedb_about.instruction import GeneralInstructions | ||
| from fastmcp import FastMCP | ||
| from fastmcp.tools import Tool | ||
|
|
||
| from . import __appname__ | ||
| from .tool import ( | ||
| fetch_cratedb_docs, | ||
| get_cluster_health, | ||
| get_cratedb_documentation_index, | ||
| get_table_metadata, | ||
| query_sql, | ||
| ) | ||
|
|
||
| instructions_general = GeneralInstructions().render() | ||
| instructions_mcp = (importlib.resources.files("cratedb_mcp") / "instructions.md").read_text() | ||
|
|
||
| # Create FastMCP application object. | ||
| mcp: FastMCP = FastMCP( | ||
| name=__appname__, | ||
| instructions=instructions_mcp + instructions_general, | ||
| ) | ||
|
|
||
|
|
||
| # ------------------------------------------ | ||
| # Text-to-SQL | ||
| # ------------------------------------------ | ||
| mcp.add_tool( | ||
| Tool.from_function( | ||
| fn=get_table_metadata, | ||
| description="Return column schema and metadata for all tables stored in CrateDB. " | ||
| "Use it to inquire entities you don't know about.", | ||
| tags={"text-to-sql"}, | ||
| ) | ||
| ) | ||
| mcp.add_tool( | ||
| Tool.from_function( | ||
| fn=query_sql, | ||
| description="Send an SQL query to CrateDB and return results. " | ||
| "Only 'SELECT' queries are allowed.", | ||
| tags={"text-to-sql"}, | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| # ------------------------------------------ | ||
| # Documentation inquiry | ||
| # ------------------------------------------ | ||
| mcp.add_tool( | ||
| Tool.from_function( | ||
| fn=get_cratedb_documentation_index, | ||
| description="Get an index of CrateDB documentation links for fetching. " | ||
| "Should download docs before answering questions. " | ||
| "Has documentation title, description, and link.", | ||
| tags={"documentation"}, | ||
| ) | ||
| ) | ||
| mcp.add_tool( | ||
| Tool.from_function( | ||
| fn=fetch_cratedb_docs, | ||
| description="Download individual CrateDB documentation pages by link.", | ||
| tags={"documentation"}, | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| # ------------------------------------------ | ||
| # Health / Status | ||
| # ------------------------------------------ | ||
| mcp.add_tool( | ||
| Tool.from_function( | ||
| fn=get_cluster_health, | ||
| description="Return the health of the CrateDB cluster.", | ||
| tags={"health", "monitoring", "status"}, | ||
| ) | ||
| ) | ||
| # Is that a standard entrypoint that should be kept alive? | ||
| mcp = CrateDbMcp().mcp |
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,90 @@ | ||
| from typing import Optional | ||
|
|
||
| from fastmcp import FastMCP | ||
| from fastmcp.tools import Tool | ||
|
|
||
| from cratedb_mcp.prompt import InstructionsPrompt | ||
|
|
||
| from . import __appname__, __version__ | ||
| from .tool import ( | ||
| fetch_cratedb_docs, | ||
| get_cluster_health, | ||
| get_cratedb_documentation_index, | ||
| get_table_metadata, | ||
| query_sql, | ||
| ) | ||
|
|
||
|
|
||
| class CrateDbMcp: | ||
| """ | ||
| Small wrapper around the FastMCP API to provide instructions prompt at runtime. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| mcp: Optional[FastMCP] = None, | ||
| instructions: Optional[str] = None, | ||
| conventions: Optional[str] = None, | ||
| ) -> None: | ||
| prompt = InstructionsPrompt(instructions=instructions, conventions=conventions) | ||
| self.mcp = mcp or FastMCP( | ||
| name=__appname__, | ||
| version=__version__, | ||
| instructions=prompt.render(), | ||
| ) | ||
| self.add_tools() | ||
|
|
||
| def add_tools(self): | ||
| """Register all CrateDB MCP tools with the FastMCP instance.""" | ||
| # ------------------------------------------ | ||
| # Text-to-SQL | ||
| # ------------------------------------------ | ||
| self.mcp.add_tool( | ||
| Tool.from_function( | ||
| fn=get_table_metadata, | ||
| description="Return column schema and metadata for all tables stored in CrateDB. " | ||
| "Use it to inquire entities you don't know about.", | ||
| tags={"text-to-sql"}, | ||
| ) | ||
| ) | ||
| self.mcp.add_tool( | ||
| Tool.from_function( | ||
| fn=query_sql, | ||
| description="Send an SQL query to CrateDB and return results. " | ||
| "Only 'SELECT' queries are allowed.", | ||
| tags={"text-to-sql"}, | ||
| ) | ||
| ) | ||
|
|
||
| # ------------------------------------------ | ||
| # Documentation inquiry | ||
| # ------------------------------------------ | ||
| self.mcp.add_tool( | ||
| Tool.from_function( | ||
| fn=get_cratedb_documentation_index, | ||
| description="Get an index of CrateDB documentation links for fetching. " | ||
| "Should download docs before answering questions. " | ||
| "Has documentation title, description, and link.", | ||
| tags={"documentation"}, | ||
| ) | ||
| ) | ||
| self.mcp.add_tool( | ||
| Tool.from_function( | ||
| fn=fetch_cratedb_docs, | ||
| description="Download individual CrateDB documentation pages by link.", | ||
| tags={"documentation"}, | ||
| ) | ||
| ) | ||
|
|
||
| # ------------------------------------------ | ||
| # Health / Status | ||
| # ------------------------------------------ | ||
| self.mcp.add_tool( | ||
| Tool.from_function( | ||
| fn=get_cluster_health, | ||
| description="Return the health of the CrateDB cluster.", | ||
| tags={"health", "monitoring", "status"}, | ||
| ) | ||
| ) | ||
|
|
||
| return self | ||
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,68 @@ | ||
| import importlib.resources | ||
| import sys | ||
| from pathlib import Path | ||
| from typing import List, Optional | ||
|
|
||
| import httpx | ||
| from cratedb_about.prompt import GeneralInstructions | ||
|
|
||
|
|
||
| class InstructionsPrompt: | ||
| """ | ||
| Bundle instructions how to use MCP tools with general instructions how to work with CrateDB. | ||
|
|
||
| - MCP: https://github.com/crate/cratedb-examples/blob/7f1bc0f94d/topic/chatbot/table-augmented-generation/aws/cratedb_tag_inline_agent.ipynb?short_path=00988ad#L776-L794 | ||
| - General: https://github.com/crate/about | ||
| """ | ||
|
|
||
| def __init__(self, instructions: Optional[str] = None, conventions: Optional[str] = None): | ||
| fragments: List[str] = [] | ||
| if instructions: | ||
| fragments.append(self.load_fragment(instructions)) | ||
| else: | ||
| instructions_general = GeneralInstructions().render() | ||
| mcp_instructions_file = ( | ||
| importlib.resources.files("cratedb_mcp.prompt") / "instructions.md" | ||
| ) | ||
| if not mcp_instructions_file.is_file(): # pragma: no cover | ||
| raise FileNotFoundError(f"MCP instructions file not found: {mcp_instructions_file}") | ||
| instructions_mcp = mcp_instructions_file.read_text() | ||
| fragments.append(instructions_general) | ||
| fragments.append(instructions_mcp) | ||
| if conventions: | ||
| fragments.append(self.load_fragment(conventions)) | ||
| self.fragments = fragments | ||
|
|
||
| def render(self) -> str: | ||
| return "\n\n".join(map(str.strip, self.fragments)) | ||
|
|
||
| @staticmethod | ||
| def load_fragment(fragment: str) -> str: | ||
| """ | ||
| Load instruction fragment from various sources. | ||
|
|
||
| Supports loading from: | ||
| - HTTP(S) URLs | ||
| - Local file paths | ||
| - Standard input (when fragment is "-") | ||
| - Direct string content | ||
|
|
||
| That's a miniature variant of a "fragment" concept, | ||
| adapted from `llm` [1] written by Simon Willison. | ||
|
|
||
| [1] https://github.com/simonw/llm | ||
| """ | ||
| try: | ||
| if fragment.startswith("http://") or fragment.startswith("https://"): | ||
| with httpx.Client(follow_redirects=True, max_redirects=3, timeout=5.0) as client: | ||
| response = client.get(fragment) | ||
| response.raise_for_status() | ||
| return response.text | ||
| if fragment == "-": | ||
| return sys.stdin.read() | ||
| path = Path(fragment) | ||
| if path.exists(): | ||
| return path.read_text(encoding="utf-8") | ||
| return fragment | ||
| except (httpx.HTTPError, OSError, UnicodeDecodeError) as e: | ||
| raise ValueError(f"Failed to load fragment '{fragment}': {e}") from e |
File renamed without changes.
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
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.