Create a lightweight MCP server that delegates to the daemon.
(project_root: str)
| 57 | |
| 58 | |
| 59 | def create_mcp_server(project_root: str) -> FastMCP: |
| 60 | """Create a lightweight MCP server that delegates to the daemon.""" |
| 61 | mcp = FastMCP("cocoindex-code", instructions=_MCP_INSTRUCTIONS) |
| 62 | |
| 63 | @mcp.tool( |
| 64 | name="search", |
| 65 | description=( |
| 66 | "Semantic code search across the entire codebase" |
| 67 | " -- finds code by meaning, not just text matching." |
| 68 | " Use this instead of grep/glob when you need to find implementations," |
| 69 | " understand how features work," |
| 70 | " or locate related code without knowing exact names or keywords." |
| 71 | " Accepts natural language queries" |
| 72 | " (e.g., 'authentication logic', 'database connection handling')" |
| 73 | " or code snippets." |
| 74 | " Returns matching code chunks with file paths," |
| 75 | " line numbers, and relevance scores." |
| 76 | " Start with a small limit (e.g., 5);" |
| 77 | " if most results look relevant, use offset to paginate for more." |
| 78 | ), |
| 79 | ) |
| 80 | async def search( |
| 81 | query: str = Field( |
| 82 | description=( |
| 83 | "Natural language query or code snippet to search for." |
| 84 | " Examples: 'error handling middleware'," |
| 85 | " 'how are users authenticated'," |
| 86 | " 'database connection pool'," |
| 87 | " or paste a code snippet to find similar code." |
| 88 | ) |
| 89 | ), |
| 90 | limit: int = Field( |
| 91 | default=5, |
| 92 | ge=1, |
| 93 | le=100, |
| 94 | description="Maximum number of results to return (1-100)", |
| 95 | ), |
| 96 | offset: int = Field( |
| 97 | default=0, |
| 98 | ge=0, |
| 99 | description="Number of results to skip for pagination", |
| 100 | ), |
| 101 | refresh_index: bool = Field( |
| 102 | default=True, |
| 103 | description=( |
| 104 | "Whether to incrementally update the index before searching." |
| 105 | " Set to False for faster consecutive queries" |
| 106 | " when the codebase hasn't changed." |
| 107 | ), |
| 108 | ), |
| 109 | languages: list[str] | None = Field( |
| 110 | default=None, |
| 111 | description="Filter by programming language(s). Example: ['python', 'typescript']", |
| 112 | ), |
| 113 | paths: list[str] | None = Field( |
| 114 | default=None, |
| 115 | description=( |
| 116 | "Filter by file path pattern(s) using GLOB wildcards (* and ?)." |