Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a new suite of commands for interacting with the Developer Knowledge API, including developerknowledge:answer, developerknowledge:documents:get, and developerknowledge:documents:search. These commands allow users to query, retrieve, and search Google developer documentation. The implementation includes necessary API clients, command definitions, and unit tests. I have reviewed the code and provided feedback on improving URL normalization and implementing defensive programming with optional chaining to handle potential undefined API responses, aligning with the repository's strict null check requirements.
| } | ||
|
|
||
| // Strip URL scheme and fragment if a full URL was supplied. | ||
| normalized = normalized.replace(/^https?:\/\//i, "").replace(/#.*$/, ""); |
There was a problem hiding this comment.
The current URL normalization only strips fragments (using #.*$), but does not strip query parameters (e.g., ?hl=en). This can result in non-canonical document names like documents/firebase.google.com/docs/cli?hl=en. We should strip both query parameters and fragments to ensure canonical document names.
| normalized = normalized.replace(/^https?:\/\//i, "").replace(/#.*$/, ""); | |
| normalized = normalized.replace(/^https?:\/\//i, "").replace(/[?#].*$/, ""); |
| const pageResults = res.body.results || []; | ||
| results.push(...pageResults); | ||
|
|
||
| if (!res.body.nextPageToken || pageResults.length === 0) { | ||
| break; | ||
| } | ||
| pageToken = res.body.nextPageToken; |
There was a problem hiding this comment.
If the API returns an empty response or if res.body is undefined, accessing res.body.results or res.body.nextPageToken will throw a TypeError. Using optional chaining ensures defensive programming and robust error handling.
| const pageResults = res.body.results || []; | |
| results.push(...pageResults); | |
| if (!res.body.nextPageToken || pageResults.length === 0) { | |
| break; | |
| } | |
| pageToken = res.body.nextPageToken; | |
| const pageResults = res.body?.results || []; | |
| results.push(...pageResults); | |
| if (!res.body?.nextPageToken || pageResults.length === 0) { | |
| break; | |
| } | |
| pageToken = res.body?.nextPageToken; |
References
- Use strict null checks and handle undefined/null explicitly. (link)
| const projectId = getProjectId(options); | ||
|
|
||
| const response = await batchGetDocuments(normalizedNames, { view, projectId }); | ||
| const documents = response.documents || []; |
There was a problem hiding this comment.
If batchGetDocuments returns an undefined body, response will be undefined, and accessing response.documents will throw a TypeError. We should use optional chaining to handle this safely.
| const documents = response.documents || []; | |
| const documents = response?.documents || []; |
References
- Use strict null checks and handle undefined/null explicitly. (link)
| const seen = new Set<string>(); | ||
| const sources: SourceInfo[] = []; | ||
|
|
||
| for (const ref of response.answer?.references || []) { |
There was a problem hiding this comment.
If response is undefined, accessing response.answer will throw a TypeError. We should use optional chaining to handle this safely.
| for (const ref of response.answer?.references || []) { | |
| for (const ref of response?.answer?.references || []) { |
References
- Use strict null checks and handle undefined/null explicitly. (link)
| projectId, | ||
| }); | ||
|
|
||
| const answerText = response.answer?.answerText?.trim(); |
There was a problem hiding this comment.
If response is undefined, accessing response.answer will throw a TypeError. We should use optional chaining to handle this safely.
| const answerText = response.answer?.answerText?.trim(); | |
| const answerText = response?.answer?.answerText?.trim(); |
References
- Use strict null checks and handle undefined/null explicitly. (link)
| function parseLimit(limitOpt?: string): number { | ||
| if (limitOpt === undefined) { | ||
| return DEFAULT_SEARCH_LIMIT; | ||
| } | ||
| const trimmed = limitOpt.trim(); |
There was a problem hiding this comment.
If limitOpt is passed programmatically as a number, calling limitOpt.trim() will throw a TypeError. We should accept both string and number and safely convert to string before trimming.
| function parseLimit(limitOpt?: string): number { | |
| if (limitOpt === undefined) { | |
| return DEFAULT_SEARCH_LIMIT; | |
| } | |
| const trimmed = limitOpt.trim(); | |
| function parseLimit(limitOpt?: string | number): number { | |
| if (limitOpt === undefined) { | |
| return DEFAULT_SEARCH_LIMIT; | |
| } | |
| const trimmed = String(limitOpt).trim(); |
Description
Adds CLI commands to query Google's official Developer Knowledge API (
developerknowledge.googleapis.com/v1), bringing parity with the existing Firebase MCPdeveloperknowledgetools andgcloud developer-knowledge:firebase developerknowledge:documents:search <query>— searches documentation chunks viaGET /v1/documents:searchDocumentChunkswith--filter <expression>(AIP-160, defaults todata_source = "firebase.google.com"; pass--filter ""to search all Google developer docs) and--limit <number>(default5, auto-paginated acrossnextPageToken).firebase developerknowledge:documents:get <names...>— retrieves full Markdown content or metadata for 1–20 documents viaGET /v1/documents:batchGetwith--view <basic|full>(defaults tofull). Accepts canonicaldocuments/{uri_without_scheme}resource names as well as fullhttps://...documentation URLs (stripping scheme, query parameters, and fragments).firebase developerknowledge:answer <query>— answers a natural language question using grounded generation viaPOST /v1:answerQuerywith--filter <expression>(defaults todata_source = "firebase.google.com"; pass--filter ""to ground across all Google developer docs), printing the answer and deduplicated authoritative source references.Auth & project behavior (matching
OneMcpServerinsrc/mcp/onemcp/index.ts):requireAuth, using the existingcloud-platformOAuth scope).developerknowledge:documents:searchanddeveloperknowledge:documents:getaccept an optional active project: when a project is selected (--projector.firebaserc), the CLI ensuresdeveloperknowledge.googleapis.comis enabled and sendsx-goog-user-project; when omitted, requests work withfirebase logincredentials (and surface an actionable--projecthint if ADC requires a quota project).developerknowledge:answerrequires an active project (needProjectId) for API quota attribution becauseAnswerQueryhas a strict per-project daily/minute quota.(Draft PR — pending Firebase API Council review.)
Scenarios Tested
47 passing):src/gcp/developerknowledge.spec.ts: document name normalization (documents/...,https://...?hl=en#frag, bare host/path, validation),--viewparsing (basic,full, defaultDOCUMENT_VIEW_FULL, rejecting invalid values), defaultdata_source = "firebase.google.com"filter &--filter ""override, search pagination, optional vs explicitx-goog-user-project&ensure(), 403 quota-project hint, repeatednamesquery params forbatchGet,POST /v1:answerQuery, and query/filter/batch size validation.src/developerknowledge/utils.spec.ts:parseLimit(string & numeric),formatChunk,formatDocument, andextractUniqueSources.src/commands/developerknowledge-documents-search.spec.ts,developerknowledge-documents-get.spec.ts,developerknowledge-answer.spec.ts: flag parsing,requireAuthregistration, formatting, missing-doc warnings/errors, source deduplication, and required project enforcement onanswer.src/commands/index.spec.ts: command tree registration and lazy loading.npm run build, ESLint (0 errors, 0 warnings on new files), and Prettier (--check) all pass.developerknowledge.googleapis.com/v1:--helpoutput for all three commands.developerknowledge:documents:searchdefaults tofirebase.google.comeven on generic queries ("Cloud Run deployment"), and--filter ""searches across all Google developer docs (docs.cloud.google.com).developerknowledge:documents:getdefaults toDOCUMENT_VIEW_FULLwith full content, supports--view basic, rejects--view content, supports batched (2 docs including a fullhttps://URL) inputs,--json, and 404 error handling on non-existent documents.developerknowledge:answer(human-readable with deduplicated sources & required-project error when no project is active).Sample Commands