How to Connect Claude Code to Your CMS with MCP
How to Connect Claude Code to Your CMS with MCP
如何通过 MCP 将 Claude Code 连接到你的 CMS
Claude Code is fluent in your repository and blind to your content. It can refactor the component that renders your blog, but ask it to publish the post that component displays and it has nowhere to look. The Model Context Protocol (MCP) closes that gap. It gives Claude Code a set of tools it can call against your CMS directly, so reading and writing content happens in the same conversation as the code. This guide connects Claude Code to a Cosmic bucket. With the hosted endpoint it takes about five minutes and requires no install.
Claude Code 虽然精通你的代码仓库,却无法感知你的内容。它可以重构渲染博客的组件,但如果你要求它发布该组件所展示的文章,它却无从下手。模型上下文协议(MCP)填补了这一空白。它为 Claude Code 提供了一套可以直接调用 CMS 的工具,从而让内容的读取和写入与代码编写在同一个对话中完成。本指南将介绍如何将 Claude Code 连接到 Cosmic 存储桶(Bucket)。使用托管端点,整个过程只需约五分钟,且无需安装任何软件。
What MCP actually gives Claude Code
MCP 实际上为 Claude Code 提供了什么
MCP is an open protocol for exposing tools to AI assistants. The Cosmic MCP server implements it and exposes 18 tools across four areas:
- Objects (5 tools): list, get, create, update, and delete content
- Media (4 tools): list, get, upload, and delete files
- Object Types (5 tools): list, get, create, update, and delete content models
- AI Generation (4 tools): generate text, images, video, and audio into your bucket
MCP 是一种用于向 AI 助手开放工具的开放协议。Cosmic MCP 服务器实现了该协议,并涵盖了四个领域的 18 种工具:
- 对象(5 种工具): 列出、获取、创建、更新和删除内容
- 媒体(4 种工具): 列出、获取、上传和删除文件
- 对象类型(5 种工具): 列出、获取、创建、更新和删除内容模型
- AI 生成(4 种工具): 在你的存储桶中生成文本、图像、视频和音频
Once connected, “publish the MCP draft and generate a hero image for it” resolves to real tool calls against your bucket. No browser tab, no copy-paste, no manual export.
连接成功后,“发布 MCP 草稿并为其生成一张主图”这样的指令将转化为针对你存储桶的真实工具调用。无需切换浏览器标签页,无需复制粘贴,也无需手动导出。
There are two ways to connect:
有两种连接方式:
-
Hosted MCP (recommended): point your client at
https://mcp.cosmicjs.com/v1/buckets/{your-bucket-slug}and authenticate with your bucket keys. Nothing to install. -
Self-hosted (stdio): run the
@cosmicjs/mcpnpm package locally vianpx. Useful for offline work, or when you want the MCP process running inside your own dev environment. -
托管式 MCP(推荐): 将你的客户端指向
https://mcp.cosmicjs.com/v1/buckets/{your-bucket-slug}并使用你的存储桶密钥进行身份验证。无需安装任何东西。 -
自托管式(stdio): 通过
npx在本地运行@cosmicjs/mcpnpm 包。适用于离线工作,或者当你希望 MCP 进程在自己的开发环境中运行时。
Before you start
开始之前
You need three things:
- A Cosmic bucket. The Free plan is $0/month and includes 1 Bucket, 2 team members, and 1,000 Objects, which is more than enough to follow along.
- Your bucket slug, read key, and write key. Step 1 below covers where to find them.
- Claude Code installed. The hosted path needs no local runtime at all. Node is only required if you choose the self-hosted stdio option, since that runs through
npx.
你需要准备三样东西:
- 一个 Cosmic 存储桶。 免费套餐为每月 0 美元,包含 1 个存储桶、2 名团队成员和 1,000 个对象,足以完成本教程。
- 你的存储桶 Slug、读取密钥和写入密钥。 下文第 1 步将介绍如何找到它们。
- 已安装 Claude Code。 托管路径完全不需要本地运行时。只有当你选择自托管 stdio 选项时才需要 Node,因为它通过
npx运行。
Step 1: Get your bucket credentials
第 1 步:获取存储桶凭据
-
Log in to the Cosmic dashboard
-
Navigate to your bucket
-
Go to Settings -> API Access
-
Copy your bucket slug, read key, and write key
-
登录 Cosmic 控制面板
-
导航到你的存储桶
-
进入 Settings -> API Access
-
复制你的存储桶 Slug、读取密钥和写入密钥
A recommendation before you paste anything: start with the read key only. Cosmic issues separate read and write keys per bucket, so you can give Claude Code full visibility into your content while making it structurally incapable of changing it. Add the write key once you trust the setup.
在粘贴任何内容之前,建议先仅使用读取密钥。Cosmic 为每个存储桶分别颁发读取和写入密钥,因此你可以让 Claude Code 完全查看你的内容,同时在结构上使其无法更改内容。当你信任该设置后,再添加写入密钥。
Step 2: Connect with hosted MCP (recommended)
第 2 步:使用托管式 MCP 连接(推荐)
Claude Code picks up project-scoped MCP servers from a .mcp.json file at the root of your repository. Create it with:
Claude Code 会从你代码仓库根目录下的 .mcp.json 文件中读取项目范围的 MCP 服务器配置。请创建该文件并填入:
{
"mcpServers": {
"cosmic": {
"url": "https://mcp.cosmicjs.com/v1/buckets/your-bucket-slug",
"headers": {
"Authorization": "Bearer your-read-key:your-write-key"
}
}
}
}
Replace your-bucket-slug, your-read-key, and your-write-key with the values from Step 1. The endpoint supports the streamable-HTTP MCP transport.
将 your-bucket-slug、your-read-key 和 your-write-key 替换为第 1 步中的值。该端点支持可流式传输的 HTTP MCP 传输协议。
Step 3: Verify the connection
第 3 步:验证连接
Restart Claude Code and run: /mcp
You should see cosmic listed with its tools. Then confirm it can actually reach your bucket by asking for something only your bucket knows:
“List all object types in my Cosmic bucket”
重启 Claude Code 并运行:/mcp
你应该能看到 cosmic 及其工具列表。然后,通过询问只有你的存储桶才知道的信息来确认它是否真的能连接到你的存储桶:
“List all object types in my Cosmic bucket”(列出我 Cosmic 存储桶中的所有对象类型)
Claude Code should call cosmic_types_list and return your real content models. If it returns your object types, the connection is live and correctly authenticated.
Claude Code 应该会调用 cosmic_types_list 并返回你真实的内容模型。如果它返回了你的对象类型,说明连接已激活且身份验证正确。