Genkit 工具开发
developing-genkit-tooling
编写 Genkit 工具的最佳实践,包括 CLI 命令和 MCP 服务器工具。涵盖命名约定、架构模式和一致性准则。
- License 许可
- Apache-2.0
- 所属仓库
- genkit-ai/genkit
- 最近更新
- 2026-11-14
- Stars
- ★ 6.6k
- Forks
- 881
01 前言 Front Matter
- name
- developing-genkit-tooling
- description
- Best practices for authoring Genkit tooling, including CLI commands and MCP server tools. Covers naming conventions, architectural patterns, and consistency guidelines.
03 SKILL.md 正文
Developing Genkit Tooling
Naming Conventions
Consistency in naming helps users and agents navigate the tooling.
CLI Commands
Use kebab-case with colon separators for subcommands.
- Format:
noun:verborcategory:action - Examples:
flow:run,eval:run,init - Arguments: Use camelCase in code (
flowName) but standard format in help text (<flowName>).
MCP Tools
Use snake_case for tool names to align with MCP standards.
- Format:
verb_noun - Examples:
list_flows,run_flow,list_genkit_docs,read_genkit_docs
CLI Command Architecture
Commands are implemented in cli/src/commands/ using commander.
Runtime Interaction
Most commands require interacting with the user's project runtime. Use the runWithManager utility to handle the lifecycle of the runtime process.
import { runWithManager } from '../utils/manager-utils';
// ... command definition ...
.action(async (arg, options) => {
await runWithManager(await findProjectRoot(), async (manager) => {
// Interact with manager here
const result = await manager.runAction({ key: arg });
});
});
Output Formatting
- Logging: Use
loggerfrom@genkit-ai/tools-common/utils. - Machine Readable: Provide options for JSON output or file writing when the command produces data.
- Streaming: If the operation supports streaming (like
flow:run), provide a--streamflag and pipe output to stdout.
MCP Tool Architecture
MCP tools in cli/src/mcp/ follow two distinct patterns: Static and Runtime.
Static Tools (e.g., Docs)
These tools do not require a running Genkit project context.
- Registration:
defineDocsTool(server: McpServer) - Dependencies: Only the
serverinstance. - Use Case: Documentation, usage guides, global configuration.
Runtime Tools (e.g., Flows, Runtime Control)
These tools interact with a specific Genkit project's runtime.
- Registration:
defineRuntimeTools(server: McpServer, options: McpToolOptions) - Dependencies: Requires
optionscontainingmanager(process manager) andprojectRoot. - Schema: MUST use
getCommonSchema(options.explicitProjectRoot, ...)to ensure the tool can accept aprojectRootargument when required (e.g., in multi-project environments).
// Runtime tool definition pattern
server.registerTool(
'my_runtime_tool',
{
inputSchema: getCommonSchema(options.explicitProjectRoot, {
myArg: z.string(),
}),
},
async (opts) => {
// Resolve project root before action
const rootOrError = resolveProjectRoot(
options.explicitProjectRoot,
opts,
options.projectRoot
);
if (typeof rootOrError !== 'string') return rootOrError;
// access manager via options.manager
}
);
Error Handling
MCP tools should generally catch errors and return them as content blocks with isError: true rather than throwing exceptions, which ensures the client receives a structured error response.
try {
// operation
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
return {
isError: true,
content: [{ type: 'text', text: `Error: ${message}` }],
};
}
04 同仓库更多 Skills
pytest 测试编写
test-writer
如何为本工作区中的模块编写 pytest 测试。在准备编写或扩展测试时加载。
Python 编码规范
python-expert
编写整洁、地道的 Python 代码的约定。读取、编辑或编写 Python 源文件时加载。
TypeScript 编码规范
typescript
TypeScript 编码约定、最佳实践,以及编写整洁、可维护代码的模式。
通用编码技能
coding
一项适用于所有编码任务的通用编码技能。
莎士比亚风格回复
shakespeare
以威廉·莎士比亚的风格回应——使用早期现代英语,富有诗意的韵律节奏。
海盗风格回复
pirate
以潇洒冒险的海盗口吻进行回应。