-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[MCP] firestore_query_collection #8548
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
+283
−2
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e55e3d3
[MCP] firestore_query_collection
nohe427 0c20c72
Whoops, debug logs
nohe427 5a7bd7d
Merge branch 'master' into mcp_firestore_query_collection
nohe427 46bfde0
Add composite query support
nohe427 312a851
Composite index, works with gemini and genkit, throws error message t…
nohe427 78dbf82
Merge branch 'master' into mcp_firestore_query_collection
nohe427 918129d
format fix
nohe427 ccb00e9
Merge branch 'master' into mcp_firestore_query_collection
nohe427 5316173
Add a comment
nohe427 6943630
Merge branch 'master' into mcp_firestore_query_collection
nohe427 f2e9998
Merge branch 'master' into mcp_firestore_query_collection
nohe427 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { z } from "zod"; | ||
import { tool } from "../../tool.js"; | ||
import { mcpError, toContent } from "../../util.js"; | ||
import { queryCollection, StructuredQuery } from "../../../gcp/firestore.js"; | ||
import { convertInputToValue, firestoreDocumentToJson } from "./converter.js"; | ||
|
||
export const query_collection = tool( | ||
{ | ||
name: "query_collection", | ||
description: | ||
"Retrieves one or more Firestore documents from a collection is a database in the current project by a collection with a full document path. Use this if you know the exact path of a collection and the filtering clause you would like for the document.", | ||
inputSchema: z.object({ | ||
// TODO: Support configurable database | ||
// database: z | ||
// .string() | ||
// .nullish() | ||
// .describe("Database id to use. Defaults to `(default)` if unspecified."), | ||
collectionPath: z | ||
.string() | ||
.describe( | ||
"A collection path (e.g. `collectionName/` or `parentCollection/parentDocument/collectionName`)", | ||
), | ||
filters: z | ||
.object({ | ||
compareValue: z | ||
.object({ | ||
stringValue: z.string().nullish().describe("The string value to compare against."), | ||
booleanValue: z.string().nullish().describe("The boolean value to compare against."), | ||
stringArrayValue: z | ||
.array(z.string()) | ||
.nullish() | ||
.describe("The string value to compare against."), | ||
integerValue: z.number().nullish().describe("The integer value to compare against."), | ||
doubleValue: z.number().nullish().describe("The double value to compare against."), | ||
}) | ||
.describe("One and only one value may be specified per filters object."), | ||
field: z.string().describe("the field searching against"), | ||
op: z | ||
.enum([ | ||
"OPERATOR_UNSPECIFIED", | ||
"LESS_THAN", | ||
"LESS_THAN_OR_EQUAL", | ||
"GREATER_THAN", | ||
"GREATER_THAN_OR_EQUAL", | ||
"EQUAL", | ||
"NOT_EQUAL", | ||
"ARRAY_CONTAINS", | ||
"ARRAY_CONTAINS_ANY", | ||
"IN", | ||
"NOT_IN", | ||
]) | ||
.describe("the equality evaluator to use"), | ||
}) | ||
.array() | ||
.describe("the multiple filters to use in querying against the existing collection."), | ||
order: z | ||
.object({ | ||
orderBy: z.string().describe("the field to order by"), | ||
orderByDirection: z | ||
.enum(["ASCENDING", "DESCENDING", "DIRECTION_UNSPECIFIED"]) | ||
.describe("the direction to order values"), | ||
}) | ||
.nullish(), | ||
limit: z | ||
.number() | ||
.describe("The maximum amount of records to return. Default is 10.") | ||
.nullish(), | ||
}), | ||
annotations: { | ||
title: "Query Firestore collection", | ||
readOnlyHint: true, | ||
}, | ||
_meta: { | ||
requiresAuth: true, | ||
requiresProject: true, | ||
}, | ||
}, | ||
async ({ collectionPath, filters, order, limit }, { projectId }) => { | ||
// database ??= "(default)"; | ||
|
||
if (!collectionPath || !collectionPath.length) | ||
return mcpError("Must supply at least one collection path."); | ||
|
||
const structuredQuery: StructuredQuery = { | ||
from: [{ collectionId: collectionPath, allDescendants: false }], | ||
}; | ||
if (filters) { | ||
structuredQuery.where = { | ||
compositeFilter: { | ||
op: "AND", | ||
filters: filters.map((f) => { | ||
if ( | ||
f.compareValue.booleanValue && | ||
f.compareValue.doubleValue && | ||
f.compareValue.integerValue && | ||
f.compareValue.stringArrayValue && | ||
f.compareValue.stringValue | ||
) { | ||
throw mcpError("One and only one value may be specified per filters object."); | ||
} | ||
const out = Object.entries(f.compareValue).filter(([, value]) => { | ||
return value !== null && value !== undefined; | ||
}); | ||
return { | ||
fieldFilter: { | ||
field: { fieldPath: f.field }, | ||
op: f.op, | ||
value: convertInputToValue(out[0][1]), | ||
}, | ||
}; | ||
}), | ||
}, | ||
}; | ||
} | ||
if (order) { | ||
structuredQuery.orderBy = [ | ||
{ | ||
field: { fieldPath: order.orderBy }, | ||
direction: order.orderByDirection, | ||
}, | ||
]; | ||
} | ||
structuredQuery.limit = limit ? limit : 10; | ||
|
||
const { documents } = await queryCollection(projectId!, structuredQuery); | ||
|
||
const docs = documents.map(firestoreDocumentToJson); | ||
|
||
const docsContent = toContent(docs); | ||
|
||
return docsContent; | ||
}, | ||
); |
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.