#16284: fix(feishu): route tool credentials by account parameter
channel: feishu
stale
size: L
Cluster:
Feishu Integration Enhancements
## Summary
This PR fixes multi-account credential isolation for the official Feishu extension by allowing callers to explicitly select which Feishu account to use per tool invocation.
## Problem
In multi-account setups, Feishu tools previously resolved a single client at registration time (effectively locking to the first enabled account). This caused cross-account operations and permission issues.
## Solution
- Add optional `account?: string` to all Feishu tool schemas (doc/wiki/drive/perm/bitable)
- Introduce `resolveToolClient(cfg, accountId?)` to resolve the Feishu client at execution time:
- If `account` is provided: use that configured + enabled account
- If omitted: fall back to the first enabled account (preserves prior behavior)
- Update tool handlers to use `resolveToolClient()` instead of a closure-captured client
- Migrate bitable tool registration away from legacy direct config access to the standard `listEnabledFeishuAccounts()` pattern
Replaces #15241 (closed due to deleted fork).
## Reviewer notes
- Additive & backward-compatible: omitting `account` keeps existing behavior
- Clear error messages for unconfigured / disabled `account` IDs
- Changes are scoped to `extensions/feishu/**` only
## Test plan
- [x] CI is green
- [x] New `extensions/feishu/src/tools-account.test.ts` covers: explicit account routing, fallback behavior, unconfigured/disabled errors, schema validation
_Reviewer focus: schema additions + account resolution logic in `resolveToolClient()`._
## Greptile Overview
### Greptile Summary
Added optional account parameter to all Feishu tool schemas (doc, wiki, drive, perm, bitable) to enable multi-account credential routing. Introduced shared resolveToolClient() helper in accounts.ts that resolves the correct client from the named account with clear error handling. When the account parameter is omitted, tools fall back to using the first enabled account, preserving backward compatibility.
### Key changes:
- New resolveToolClient() function in accounts.ts:152 centralizes account resolution logic with proper error messages for unconfigured/disabled accounts
- All tool schemas updated with consistent AccountField definition (optional string parameter)
- Bitable tools migrated from legacy direct config access to standard listEnabledFeishuAccounts() pattern
- Comprehensive test coverage in tools-account.test.ts validates explicit account resolution, fallback behavior, and error cases
## Confidence Score: 5/5
- This PR is safe to merge with minimal risk
- The implementation is well-structured with proper error handling, comprehensive test coverage (130 lines of tests covering all critical paths), and maintains backward compatibility. The resolveToolClient() helper provides clear error messages for edge cases. All changes follow consistent patterns across tool files. The only minor concern is the inclusion of unrelated web-fetch commits, but these are also well-tested and don't affect the core Feishu functionality.
- No files require special attention
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Adds optional `account` parameter to all Feishu tool schemas (doc, wiki, drive, perm, bitable) and introduces a shared `resolveToolClient()` helper in `accounts.ts` to route tool invocations to the correct Feishu account at execution time. When `account` is omitted, the tool falls back to the invoking bot's account (via `ctx.agentAccountId`) then to the first enabled account, preserving backward compatibility. Bitable tools are also migrated from legacy direct config access to the standard `listEnabledFeishuAccounts()` registration pattern.
- New `resolveToolClient()` centralizes account resolution with clear error messages for unconfigured/disabled accounts
- All tool registrations migrated from static `getClient()` closures to dynamic per-invocation resolution via the `(ctx) => { ... }` factory pattern
- `mediaMaxBytes` in `docx.ts` now resolves from the per-invocation account config rather than being fixed to the first account at registration time
- Comprehensive tests cover explicit account routing, preferred account fallback, error cases, and schema validation
<h3>Confidence Score: 4/5</h3>
- This PR is safe to merge with minimal risk; the changes are additive, backward-compatible, and well-tested.
- The implementation is well-structured with proper error handling, comprehensive test coverage, and maintains backward compatibility. The `resolveToolClient()` helper provides clear error messages for edge cases and all tool files follow a consistent pattern. One minor style concern around repeated `AccountField` definitions is non-blocking. No logical errors found.
- No files require special attention
<sub>Last reviewed commit: e2cb790</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#19871: fix(feishu): support multi-account config in bitable tools
by linbojin · 2026-02-18
87.4%
#15671: fix(feishu): Credentials check bug of Feishu bitable tools when usi...
by zxh0916 · 2026-02-13
86.3%
#23261: fix(feishu): bitable tools fail to register with accounts config pa...
by stakeswky · 2026-02-22
84.7%
#23255: fix(feishu): use listEnabledFeishuAccounts for bitable tool registr...
by SidQin-cyber · 2026-02-22
81.3%
#16337: feat(feishu): sync community plugin updates from clawdbot-feishu v0...
by graydawnc · 2026-02-14
79.0%
#14674: feat(feishu): add feishu_chat tool for chat info/member queries
by liuweifly · 2026-02-12
76.7%
#20801: fix: auto-select enabled account when only one is available
by mariusorani · 2026-02-19
75.1%
#21484: fix(feishu): scope message deduplication by accountId to support mu...
by guanyu-zhang · 2026-02-20
73.9%
#16064: feat: add contact-based tool permissions with verification
by jamiequint · 2026-02-14
73.2%
#23333: fix(matrix): add accountId routing for multi-account message sending 🤖
by BadTurki · 2026-02-22
71.5%