-
Notifications
You must be signed in to change notification settings - Fork 208
fix(ask): Properly handle regex filters that include parenthesis #786
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| // Entry point for the MCP server | ||
| import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; | ||
| import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; | ||
| import { preprocessRegexp } from '@sourcebot/shared'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: mcp cannot take a dependency on |
||
| import escapeStringRegexp from 'escape-string-regexp'; | ||
| import { z } from 'zod'; | ||
| import { listRepos, search, getFileSource } from './client.js'; | ||
|
|
@@ -25,7 +26,8 @@ server.tool( | |
| If you receive an error that indicates that you're not authenticated, please inform the user to set the SOURCEBOT_API_KEY environment variable. | ||
| If the \`includeCodeSnippets\` property is true, code snippets containing the matches will be included in the response. Only set this to true if the request requires code snippets (e.g., show me examples where library X is used). | ||
| When referencing a file in your response, **ALWAYS** include the file's external URL as a link. This makes it easier for the user to view the file, even if they don't have it locally checked out. | ||
| **ONLY USE** the \`filterByRepoIds\` property if the request requires searching a specific repo(s). Otherwise, leave it empty.`, | ||
| **ONLY USE** the \`filterByRepoIds\` property if the request requires searching a specific repo(s). Otherwise, leave it empty. | ||
| If the request is asking to search for a specific file or results for files in a specific file path, **YOU MUST** ensure that the \`filterByFile\` property is used.`, | ||
| { | ||
| query: z | ||
| .string() | ||
|
|
@@ -41,6 +43,10 @@ server.tool( | |
| .array(z.string()) | ||
| .describe(`Scope the search to the provided languages. The language MUST be formatted as a GitHub linguist language. Examples: Python, JavaScript, TypeScript, Java, C#, C++, PHP, Go, Rust, Ruby, Swift, Kotlin, Shell, C, Dart, HTML, CSS, PowerShell, SQL, R`) | ||
| .optional(), | ||
| filterByFile: z | ||
| .array(z.string()) | ||
| .describe("Scope the search to results inside filepaths that match the provided regex expression. By default all files are searched, so **only use this filter if you need to filter on specific files**. **YOU MUST** ensure that this is a valid regex expression and any special characters are properly escaped. If the regex expresion includes a paranthesis **YOU MUST** wrap this value in quotes when passing it in.") | ||
| .optional(), | ||
| caseSensitive: z | ||
| .boolean() | ||
| .describe(`Whether the search should be case sensitive (default: false).`) | ||
|
|
@@ -58,6 +64,7 @@ server.tool( | |
| query, | ||
| filterByRepoIds: repoIds = [], | ||
| filterByLanguages: languages = [], | ||
| filterByFile: filePath = [], | ||
| maxTokens = env.DEFAULT_MINIMUM_TOKENS, | ||
| includeCodeSnippets = false, | ||
| caseSensitive = false, | ||
|
|
@@ -70,6 +77,11 @@ server.tool( | |
| query += ` ( lang:${languages.join(' or lang:')} )`; | ||
| } | ||
|
|
||
| if (filePath.length > 0) { | ||
| const quotedFilters = filePath.map(preprocessRegexp); | ||
| query += ` ( file:${quotedFilters.join(' or file:')} )`; | ||
| } | ||
|
|
||
| const response = await search({ | ||
| query, | ||
| matches: env.DEFAULT_MATCHES, | ||
|
|
||
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,15 @@ | ||
| /** | ||
| * Wraps a value in quotes if it contains parentheses and isn't already quoted. | ||
| * | ||
| * This is needed because the query language does not allow values to include parenthesis unless they're quoted. This is | ||
| * due to the ParenExpr symbol which parses on these parenthesis. We instruct the agent to wrap the regexp in quotes | ||
| * but it's flaky, so we due it here as well as a backup plan to ensure the parser doesn't fail. | ||
| */ | ||
| export const preprocessRegexp = (value: string): string => { | ||
| const hasParentheses = value.includes('(') || value.includes(')'); | ||
| const isAlreadyQuoted = value.startsWith('"') && value.endsWith('"'); | ||
| if (hasParentheses && !isAlreadyQuoted) { | ||
| return `"${value}"`; | ||
| } | ||
| return value; | ||
| }; |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use “parentheses” (plural) for clarity.
Minor wording nit in the changelog entry.
✏️ Suggested tweak
📝 Committable suggestion
🤖 Prompt for AI Agents