ima-mcp-cos-upload
FreeRuns bundled scriptsNot checked把本地文件(md/pdf/pptx/docx/图片等)上传导入 ima 知识库:走通 create_media → COS 签名直传 → add_knowledge 全流程,自带 403 报错自修复;md 卡片上传前可按规范自动打标(原料层三段式/资产层五维)。触发:上传文件到 ima 知识库、ima…
by dzz-777507 installs in other catalogsAbout this skill
ima-mcp 知识库 COS 直传方案
本 skill 解决 WorkBuddy 的 mcp__ima-mcp__create_media → 手动 COS PUT → add_knowledge 入库链路中反复出现的 COS 403 问题。与 ima-skills(OpenAPI ima_api.cjs)是不同路径——本链路用 MCP 服务器下发的临时 COS 凭证,需自己算 HMAC-SHA1 签名并 PUT。
环境自检(无 ima-mcp / 无凭据环境先跑这条)
python3 scripts/upload_cos.py --selftest
零凭据、零网络,8 项校验签名算法回归(sha1 算法/AK 透传/时间窗/签名头字母序/40 位 hex/确定性/请求头集合)。沙箱、CI、报错排障时先跑它:全过=算法没坏,问题在凭证或网络;有挂=实现被改坏,禁上传。无参数运行会打印两种运行方式引导。
何时用
- 用 ima-mcp 把本地
.md/.pdf/.pptx等推入「你的 ima 知识库(共享库或个人库均可)」。 - 现象:
curl返403 Forbidden(XML 报错AccessDenied/SignatureDoesNotMatch)。
链路全景
create_media(file_name, file_size, content_type, file_ext, knowledge_base_id)→ 返回cos_credential(含SecretId/SecretKey/Token/Key、桶 host)。- 用凭证把文件
PUT到 COS(自己算签名)。 add_knowledge(media_id, folder_id?)入库(media_id 来自 create_media)。
重名策略用 SAVE,新版本带
v1.x不撞名。ima-mcp 无法删除/移动/改标签/建夹——旧版清理须用户在 ima 界面手动。
🏷️ 自动打标协议(md 卡片上传前执行,AI 自行打标,无需用户手动)
核心认知:ima 无原生标签 API——标签只能写进 md 的 front-matter tags:(或文末「标签」区),随文件入库。打标是上传流程的第一步,不是可选项。
分层打标(按目标库二选一):
- 原料层(公众号/PDF/网页原文 → 资料收集类库):必打三段式 3 枚——
来源:<公众号(名)/PDF/网页>·置信度:<事实记录/较可信/案例级/待校准(二手)/待校准(二手汇编)>·蒸馏状态:<已蒸馏→X卡 / 待评估 / 暂缓(非品牌核心) / 归案例库(非方法卡)>。作用:蒸馏后回写「已蒸馏→X卡」防重复搬运,是知识库瘦身的分层开关。 - 资产层(蒸馏出的方法卡/案例卡 → 个人主库/公司库):必打五维 3–5 枚——
学科域:<1–2个>(宁缺毋滥,选错比不选糟)·方法:<1个检索键>·阶段:<洞察/方法论提炼/方案/落地/复盘>·类型:<步骤卡/原理卡/工具箱卡/案例卡/模型卡>·行业:<可选>。 - 一卡 3–5 枚足够,禁「全打」造成检索噪声;五维取值全表见
references/tagging-spec.md。
流程:读卡内容 → 判定原料/资产层 → 按 above 生成标签写入 front-matter → 再走 COS 上传。元数据卡(索引/清单类)不打五维,只打 类型:索引 即可。
⛔ 两处致命签名坑(缺一则 403)
来自 2026-08-13 实战,已验证:
坑 A — HttpString 必须尾随 \n
HttpString = "{method}\n{http_uri}\n\n{http_headers}\n" # 末项后还要一个 \n
缺最后的 \n → COS 403。这是最容易漏的一点。
坑 B — Header 值必须用 safe='' 编码(/→%2F)
http_uri(对象 Key 路径):urllib.parse.quote(key, safe='/')—— 保留/。http_headers的每个 value:urllib.parse.quote(v, safe='')—— 编码/。 二者编码规则不同,别混。
坑 C — cos_key 不含前导 /,签名 http_uri 必须手动补 /
create_media返回的cos_key不带前导/(如5/fGpy...md)。- 但 COS 服务端验签的
FormatString里http_uri带前导/(如/5/fGpy...md)。 - 若签名用
5/fGpy...md(无/)→ COS 返403 SignatureDoesNotMatch,响应体 XML 可定位根因。 - ✅ 修复:
http_uri = "/" + urllib.parse.quote(cos_key, safe='/'),即先补/再编码。 - 来自 2026-08-27 实战:4 本投资书首传全 403,抓响应体定位根因修复后重传全 200。
签名算法(Python 参考实现)
import hmac, hashlib, time, urllib.parse
def build_cos_auth(secret_id, secret_key, token, method, cos_key, content_type,
host="ima-share-kb-1258344701.cos.ap-shanghai.myqcloud.com"):
method = method.lower()
http_uri = "/" + urllib.parse.quote(cos_key, safe='/') # 坑C: cos_key 无前导/,签名必须补;坑B: 保留 '/'
headers = {
"content-type": content_type,
"host": host,
"x-cos-security-token": token,
}
http_headers = "&".join( # 坑B: value safe=''
f"{k}={urllib.parse.quote(v, safe='')}" for k, v in sorted(headers.items())
)
now = int(time.time()); expire = 600
key_time = f"{now};{now+expire}"
sign_key = hmac.new(secret_key.encode(), key_time.encode(), hashlib.sha1).hexdigest()
http_string = f"{method}\n{http_uri}\n\n{http_headers}\n" # 坑A: 尾随 \n
string_to_sign = f"sha1\n{key_time}\n{hashlib.sha1(http_string.encode()).hexdigest()}\n"
signature = hmac.new(sign_key.encode(), string_to_sign.encode(), hashlib.sha1).hexdigest()
header_list = ";".join(sorted(headers.keys()))
auth = (f"q-sign-algorithm=sha1&q-ak={secret_id}&q-sign-time={key_time}"
f"&q-key-time={key_time}&q-header-list={header_list}"
f"&q-url-param-list=&q-signature={signature}")
return auth, headers
# 用法:
# auth, headers = build_cos_auth(cred["SecretId"], cred["SecretKey"], cred["Token"],
# "put", cred["Key"], "text/markdown")
# curl --noproxy '*' -X PUT -T file \
# -H "Authorization: $auth" \
# -H "x-cos-security-token: $token" \
# -H "Content-Type: text/markdown" \
# "https://{host}/{cos_key}"
⛔ 网络约束(沙箱,2026-08-13 厘清)
- 后台 Bash 无 DNS(rc=6)→ 上传必须前台执行。
- 前台
--resolve钉死 IP 反不可达(rc=60)→ 不能用 --resolve。 - 用
curl --noproxy '*' -X PUT -T file(带鉴权头,无代理、无 --resolve),curl 自走系统 DNS。 - HTTP 正向转发对长签名头硬性 502 → 只用 HTTPS。
- 域名用直达
ima-share-kb-1258344701.cos.ap-shanghai.myqcloud.com,非 custom_domain CDN(会被 Lego Server 拒 403)。
✅ 稳路:整链委托 Agent 子代理(强烈推荐)
主会话手动把 create_media 返回的 token 抄进凭证 JSON 极易损坏(长字符串复制错位)→ 必 403。 已验证解法:把 create_media → 签名 → COS PUT → add_knowledge 整套交给一个 general-purpose Agent 子代理在它自己的上下文里跑,主会话不手动传 token。子代理首试即 HTTP 200。
调用 Agent 时把文件路径、folder_id、knowledge_base_id、content_type 写清楚,让它独立读 create_media 响应、算签名、上传、注册,最后回报 media_id 与 HTTP 状态。
⚠️ 实测:子代理偶发不可用(报
Tool Agent not found)。此时退回主会话自跑脚本同样可行——把整段 Python(含build_cos_auth)写进一个.py文件,主会话直接python script.py跑create_media→签名→curl PUT→add_knowledge,token 不手动跨消息复制即不损坏。
Install ima-mcp-cos-upload in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
FAQ
What does the ima-mcp-cos-upload skill do?
把本地文件(md/pdf/pptx/docx/图片等)上传导入 ima 知识库:走通 create_media → COS 签名直传 → add_knowledge 全流程,自带 403 报错自修复;md 卡片上传前可按规范自动打标(原料层三段式/资产层五维)。触发:上传文件到 ima 知识库、ima 导入文档、ima 推库/入库、上传报 COS 403 或 SignatureDoesNotMatch、ima 打标。
How do I install the ima-mcp-cos-upload skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the ima-mcp-cos-upload skill run scripts?
Yes, this skill bundles executable scripts. Review the source before installing.
Related skills
docx
Use this skill whenever the user wants to create, read, edit, or manipulate Word documents (.docx files) or Word templates (.dotx files). Triggers include…
by AnthropicUse this skill whenever the user wants to do anything with PDF files. This includes reading or extracting text/tables from PDFs, combining or merging multiple…
by Anthropicxlsx
Use this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing…
by Anthropicpptx
Use this skill any time a .pptx or .potx file is involved in any way — as input, output, or both. This includes: creating slide decks, pitch decks, or…
by Anthropic