Skip to content

feat(developerknowledge): add Developer Knowledge CLI commands - #11612

Draft
joehan wants to merge 4 commits into
mainfrom
joehan/developerknowledge-cli
Draft

joehan wants to merge 4 commits into
mainfrom
joehan/developerknowledge-cli

Conversation

@joehan

@joehan joehan commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Description

Adds CLI commands to query Google's official Developer Knowledge API (developerknowledge.googleapis.com/v1), bringing parity with the existing Firebase MCP developerknowledge tools and gcloud developer-knowledge:

  • firebase developerknowledge:documents:search <query> — searches documentation chunks via GET /v1/documents:searchDocumentChunks with --filter <expression> (AIP-160, defaults to data_source = "firebase.google.com"; pass --filter "" to search all Google developer docs) and --limit <number> (default 5, auto-paginated across nextPageToken).
  • firebase developerknowledge:documents:get <names...> — retrieves full Markdown content or metadata for 1–20 documents via GET /v1/documents:batchGet with --view <basic|full> (defaults to full). Accepts canonical documents/{uri_without_scheme} resource names as well as full https://... documentation URLs (stripping scheme, query parameters, and fragments).
  • firebase developerknowledge:answer <query> — answers a natural language question using grounded generation via POST /v1:answerQuery with --filter <expression> (defaults to data_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 OneMcpServer in src/mcp/onemcp/index.ts):

  • All three commands require authentication (requireAuth, using the existing cloud-platform OAuth scope).
  • developerknowledge:documents:search and developerknowledge:documents:get accept an optional active project: when a project is selected (--project or .firebaserc), the CLI ensures developerknowledge.googleapis.com is enabled and sends x-goog-user-project; when omitted, requests work with firebase login credentials (and surface an actionable --project hint if ADC requires a quota project).
  • developerknowledge:answer requires an active project (needProjectId) for API quota attribution because AnswerQuery has a strict per-project daily/minute quota.

(Draft PR — pending Firebase API Council review.)

Scenarios Tested

  • Unit tests (47 passing):
    • src/gcp/developerknowledge.spec.ts: document name normalization (documents/..., https://...?hl=en#frag, bare host/path, validation), --view parsing (basic, full, default DOCUMENT_VIEW_FULL, rejecting invalid values), default data_source = "firebase.google.com" filter & --filter "" override, search pagination, optional vs explicit x-goog-user-project & ensure(), 403 quota-project hint, repeated names query params for batchGet, POST /v1:answerQuery, and query/filter/batch size validation.
    • src/developerknowledge/utils.spec.ts: parseLimit (string & numeric), formatChunk, formatDocument, and extractUniqueSources.
    • src/commands/developerknowledge-documents-search.spec.ts, developerknowledge-documents-get.spec.ts, developerknowledge-answer.spec.ts: flag parsing, requireAuth registration, formatting, missing-doc warnings/errors, source deduplication, and required project enforcement on answer.
    • src/commands/index.spec.ts: command tree registration and lazy loading.
  • Build, lint, and formatting: npm run build, ESLint (0 errors, 0 warnings on new files), and Prettier (--check) all pass.
  • Live CLI end-to-end verification against developerknowledge.googleapis.com/v1:
    • Verified --help output for all three commands.
    • Verified developerknowledge:documents:search defaults to firebase.google.com even on generic queries ("Cloud Run deployment"), and --filter "" searches across all Google developer docs (docs.cloud.google.com).
    • Verified developerknowledge:documents:get defaults to DOCUMENT_VIEW_FULL with full content, supports --view basic, rejects --view content, supports batched (2 docs including a full https:// URL) inputs, --json, and 404 error handling on non-existent documents.
    • Verified developerknowledge:answer (human-readable with deduplicated sources & required-project error when no project is active).

Sample Commands

# Search Firebase documentation chunks (defaults to data_source = "firebase.google.com")
firebase developerknowledge:documents:search "Firestore security rules" --limit 5

# Search across Google Cloud docs or all Google developer docs
firebase developerknowledge:documents:search "Cloud Run IAM" \
  --filter 'data_source = "docs.cloud.google.com"'
firebase developerknowledge:documents:search "Cloud Run IAM" --filter ""

# Retrieve full Markdown content (default: --view full) or basic metadata for 1–20 documents
firebase developerknowledge:documents:get \
  documents/firebase.google.com/docs/cli \
  --view basic

firebase developerknowledge:documents:get \
  documents/firebase.google.com/docs/firestore/security/get-started \
  documents/firebase.google.com/docs/firestore/security/rules-structure

# Answer a Firebase question using grounded generation (requires an active project)
firebase developerknowledge:answer \
  "How do I deploy Firestore security rules with Firebase CLI?" \
  --project my-project-id

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/gcp/developerknowledge.ts Outdated
}

// Strip URL scheme and fragment if a full URL was supplied.
normalized = normalized.replace(/^https?:\/\//i, "").replace(/#.*$/, "");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
normalized = normalized.replace(/^https?:\/\//i, "").replace(/#.*$/, "");
normalized = normalized.replace(/^https?:\/\//i, "").replace(/[?#].*$/, "");

Comment thread src/gcp/developerknowledge.ts Outdated
Comment on lines +249 to +255
const pageResults = res.body.results || [];
results.push(...pageResults);

if (!res.body.nextPageToken || pageResults.length === 0) {
break;
}
pageToken = res.body.nextPageToken;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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
  1. 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 || [];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
const documents = response.documents || [];
const documents = response?.documents || [];
References
  1. 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 || []) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If response is undefined, accessing response.answer will throw a TypeError. We should use optional chaining to handle this safely.

Suggested change
for (const ref of response.answer?.references || []) {
for (const ref of response?.answer?.references || []) {
References
  1. Use strict null checks and handle undefined/null explicitly. (link)

projectId,
});

const answerText = response.answer?.answerText?.trim();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If response is undefined, accessing response.answer will throw a TypeError. We should use optional chaining to handle this safely.

Suggested change
const answerText = response.answer?.answerText?.trim();
const answerText = response?.answer?.answerText?.trim();
References
  1. Use strict null checks and handle undefined/null explicitly. (link)

Comment on lines +20 to +24
function parseLimit(limitOpt?: string): number {
if (limitOpt === undefined) {
return DEFAULT_SEARCH_LIMIT;
}
const trimmed = limitOpt.trim();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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();

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant