简单实现一个 claude plugin

背景

之前每次使用 claude code 的时候,如果上下文太多了,比如说:

  1. 多轮重构
  2. 长时间 debug
  3. PRD / 设计文档 + 代码同时存在

这些会导致效果不好。再新开一个窗口又需要重新开始,但是新开一个窗口会有以下问题:

  1. 需要重新解释项目背景
  2. 需要手动复制关键代码 / 设计说明
  3. 很容易漏掉隐含上下文(约定、命名、历史决策)

所以想着能不能实现一个命令来实现保存上下文,正好 claude code 推出了 plugins 正好可以做一个来试试看。

实现方式

从设计上看,Claude Code 的 plugin 并不是“代码插件”,而更接近一种 Skill / Context Provider。这里可以看下官方例子:

image-20251222161455566

plugin 的核心职责,并不是“执行复杂逻辑”,而是以不同形式,把能力或上下文接入到 Claude Code 的工作流中。有六种方式和 claude code 集成,这些可以组合使用:

  1. Slash command
  2. Agent
  3. Skills
  4. Hook
  5. Mcp Server
  6. LSP Server

我们实现方式是用 slash command 和 mcp-server 作为使用方法,mcp-server 具体实现我们这里不展开了,来看下 slash command 是怎么实现的。这是我们整体目录:

1
2
3
4
5
6
7
8
9
10
nidhogg-marketplace/
├── .claude-plugin/
│ └── marketplace.json
└── nidhogg/
├── .claude-plugin/
│ └── plugin.json
├── commands/
│ └── save.md
└── .mcp.json

  1. Marketplace 表示是插件集合的入口,下面可以有多个 plugin
  2. nidhogg/.claude-plugin/plugin.json 是 plugin 主体,重点关心这一段。

slash command 实现

1.首先创建一个 plugin marketplace 目录,用来存放插件定义:

1
mkdir nidhogg-marketplace

2.随后创建 Claude Code 约定的插件配置目录:

1
mkdir nidhogg-marketplace/.claude-plugin

manifest 文件表示插件的身份信息:包括名称、描述和版本,写入 manifest 信息:

1
/.claude-plugin/plugin.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"name": "",
"owner": {
"name": ""
},
"metadata": {
"description": "",
"homepage": ""
},
"plugins": [
{
"name": "nidhogg",
"version": "0.1.0",
"source": "./nidhogg",
"description": "Persistent conversation memory and structured context system for Claude Code"
}
]
}

这里需要注意的是:

  • manifest 只描述“插件是什么”
  • 并不涉及任何具体逻辑
  • Claude Code 会通过它发现并加载 plugin

3.接着创建 slash command:

1
mkdir nidhogg-marketplace/commands

4.创建具体命令,命令名就是文件名,比如我的是 save,哪就应该是 save.md:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
---
description: Save the current conversation into Nidhogg for long-term memory and future reuse
---

# Save Conversation

This conversation appears to be complete and ready to be preserved.

You should:
1. Identify the main topic and purpose of the conversation.
2. Write a concise summary suitable for future recall.
3. Extract any important decisions, conclusions, or design outcomes.
4. Assign relevant tags.
5. Call the Nidhogg MCP tool to persist:
- the full conversation as Markdown
- structured metadata as JSON

Ensure the saved output is clean, structured, and optimized for long-term retrieval.

你需要在这个 Markdown 中明确告诉模型:

  • 当前命令的目的是什么
  • 应该如何理解当前上下文
  • 需要调用哪些工具
  • 输出应该满足什么结构和质量要求

完成这个文件之后,一个 Slash command 就已经“实现”了。

4.我们可以使用 claude –plugin-dir ./nidhogg-marketplace 方式来做个测试,可以看到我们的命令:

image-20251222163604349

使用 mcp-server

我们可以进阶一步,来实现 mcp-server,我们在之前 save.md 中也提到过 mcp tool,这里比较简单,我们配置好 .mcp.json 就可以,比如像下面这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"mcpServers": {
"nidhogg-mcp": {
"type": "stdio",
"command": "uv",
"args": [""],
"cwd": "",
"env": {
"PYTHONPATH": ""
}
}
}
}

参考

  1. https://code.claude.com/docs/en/plugins
  2. https://github.com/anthropics/claude-code/tree/main/plugins/code-review
  3. https://code.claude.com/docs/en/plugins-reference
  4. https://github.com/Shuimo03/Nidhogg

简单实现一个 claude plugin
http://example.com/2025/12/14/简单实现一个-claude-plugin/
作者
Wu Liang
发布于
2025年12月14日
许可协议