Programmatic use
Install, list, and remove MCP servers from your own CLI or tool with the add-mcp SDK.
The add-mcp npm package doubles as a TypeScript SDK. If you’re building a CLI or developer tool that should set up an MCP server for its users (“add our MCP server to Cursor with one command”), the SDK gives you the same battle-tested config writing the CLI uses — every agent, every format, capability gating included.
Install
npm install add-mcppnpm add add-mcpyarn add add-mcpbun add add-mcpAdd a server to an agent
import { upsertServer } from "add-mcp";
// Remote server → project-level Cursor config (.cursor/mcp.json)
const result = upsertServer(
"cursor",
"context7",
{ type: "http", url: "https://mcp.context7.com/mcp" },
{ local: true },
);
if (!result.success) {
console.error(result.error);
}
console.log(`written to ${result.path}`);
Stdio servers work the same way:
import { upsertServer } from "add-mcp";
// npm package → global Claude Code config
upsertServer("claude-code", "postgres", {
command: "npx",
args: ["-y", "@modelcontextprotocol/server-postgres"],
});
upsertServer and removeServer return { success, path, error? } rather than throwing, so a failed write in one agent never crashes your tool.
Detect the user’s agents
import { detectProjectAgents, detectGlobalAgents } from "add-mcp";
const project = detectProjectAgents("/path/to/project"); // sync
const global = await detectGlobalAgents(); // async
Combine detection with upsertServer to offer a one-command install across everything the user actually has:
import { detectProjectAgents, upsertServer } from "add-mcp";
for (const agent of detectProjectAgents(process.cwd())) {
upsertServer(
agent,
"my-server",
{ type: "http", url: "https://mcp.example.com/mcp" },
{ local: true, cwd: process.cwd() },
);
}
List and remove
import { listInstalledServers, removeServer } from "add-mcp";
const projectServers = await listInstalledServers({ cwd: "/path/to/project" });
const globalServers = await listInstalledServers({ global: true });
removeServer("claude-code", "example", {
local: true,
cwd: "/path/to/project",
});
Next steps
- API reference — every exported function and type.