Abdulkader Safi

Giving AI agents a CRM: read-only tokens, rate limits, approvals

Private CRM has an MCP server so Claude and other agents can work in it. Tokens read by default, calls are capped, and closing a deal waits for a person.

6 min read

Share
MCP server for a CRM showing an AI agent request waiting for human approval on a deal page

The first commit of Private CRM already had an MCP endpoint. Three days later it had 37 tools and eight AI agents using it. Giving agents write access to a CRM is easy. Making sure a confused agent can't wreck it took most of the work.

This post covers the server itself, then the safety layers I added one at a time, and the idempotency rules that let agents retry without creating duplicates.

A small JSON-RPC handler

MCP over HTTP is JSON-RPC 2.0. Private CRM has two routes behind token authentication:

route.middleware(Authenticate::via::<ApiTokens>()).group(|route| {
    route.post("/mcp", mcp::store);
    route.get("/mcp", mcp::show);
});

show returns 405. The server never sends messages on its own, so there's no event stream to open. store validates the JSON-RPC envelope and hands the method to one function that matches on initialize, ping, tools/list and tools/call.

initialize returns an instructions string built at request time. It tells the agent the company currency, lists the deal stages in order, and says which tool to use for which job ("Bill with create_invoice and record money received with record_payment"). Agents read it before anything else, so it's the cheapest place to stop wrong guesses.

Tokens are stored as SHA-256 hashes and act as the user who created them. An agent sees exactly what that person sees in the browser, using the same can_see rule from part 2.

Connecting Claude Code is one command:

claude mcp add --transport http private-crm https://your-crm.example/api/mcp \
  --header "Authorization: Bearer $CRM_TOKEN"

I covered the protocol itself in introducing MCP and built an MCP server for my portfolio before this one. A CRM is different because the tools write.

Layer one: read-only by default

New tokens can only read unless their owner ticks Can make changes. A read-only token doesn't run the write tools, and it doesn't see them either:

"tools/list" => {
    let tools: Vec<Value> = tools()
        .as_array()
        .into_iter()
        .flatten()
        .filter(|tool| can_write() || !is_write_tool(tool["name"].as_str().unwrap_or_default()))
        .cloned()
        .collect();
    Ok(json!({ "tools": tools }))
}

Hiding them matters. An agent that sees update_opportunity will try it, fail, and burn turns explaining the failure. An agent that never sees it plans around it.

Tokens that existed before this change kept write access, so connected agents didn't break mid-week.

Layer two: 120 calls a minute per token

A looping agent can fire hundreds of calls before anyone notices. Each token is capped:

if let Ok(Err(_)) = rate_limit::attempt(&format!("mcp:{token}"), Limit::per_minute(120)).await {
    return StatusCode::TOO_MANY_REQUESTS.into_response();
}

The limit is per token, not per user, so one runaway agent doesn't lock out the person's other tools.

Layer three: Won and Lost wait for a person

Closing a deal changes reports, triggers follow-up work and, later, invoices. I didn't want an agent doing that on a hunch. So move_opportunity to Won or Lost doesn't move the deal. It creates a pending change and notifies the deal's owner:

if stage.is_closed() && opportunity.stage != stage {
    let change = actions::opportunities::ask_to_close(&opportunity, stage, lost_reason, lost_category, user).await?;
    return Ok(json!({
        "opportunity": opportunity_json(&opportunity),
        "pending_approval": { "id": change.id, "stage": change.stage, "lost_reason": change.lost_reason },
        "message": "Won and Lost need a person's approval. The deal moves once someone approves it on the deal page.",
    }));
}

The deal page shows the request with Approve and Reject. Approving runs the normal move, both answers go in the activity log, and a change that's already decided returns 409 if someone tries again. The message field matters too: it tells the agent what happened in words, so it reports "waiting for approval" instead of "done".

Lost also needs a reason, for people and agents alike. move_to refuses a lost move without one, so the pipeline, the deal page and MCP all follow the same rule.

Layer four: no tool can delete

None of the tools delete or archive. Part 3 showed the test that reads tools/list and fails if any tool name contains delete, remove, archive or destroy. The worst a confused agent can do is write a wrong value, and the activity log records which token wrote it.

Idempotent tools, so retries are safe

Agents retry. Mailbox syncs run twice. Two agents look for the same prospect. Every create tool in Private CRM has a rule for "already exists":

  • create_contact with an email already in the CRM returns that contact with created: false.
  • create_company with a name you can already see does the same.
  • log_activity with a Gmail message id already on the deal returns the first entry, so a sync never logs an email twice.
  • Creating a deal for an Upwork job URL already logged returns that deal. The URL is stored without tracking parameters first, so .../jobs/~01abc/?referrer_url_path=find-work and .../jobs/~01abc are one job.
  • create_service and update_service find the item by SKU and update it, or create it when the SKU is new.

Update tools follow one more rule: only the fields you send change, and null clears one. In Rust that's a Sometimes<Option<T>> field: absent, present with a value, or present and null. An agent can fix a deal's currency without resending the title.

From 12 tools to 37

The first README listed 12 tools: pipeline summary, contacts, deals, stage moves, activities, tasks and tags. The agents' own requests (part 6) added invoices, payments, projects, milestones, companies, follow-ups, reports and the services catalog. Every new write tool went into a WRITE_TOOLS list, so read-only tokens never see it.

Where to start with your own

Ship read-only first and watch what agents ask for. Put a human in front of any change that is expensive to undo, and return a plain-language message saying so. Make every create tool return the existing record instead of a duplicate. Claude Code users will recognise the same idea from its permission modes: reads are free, writes need a yes.

Part 6 is what happened when those agents started filing feature requests.

Last updated 4 Dec 2026 · filed under Rust, ai, claude, crm, agents, ai agent

Every entry on this project

12 build notes, in order

Including the ones where nothing worked. You are on part 5.

Read the series →

FAQ

Frequently asked questions

How do I connect Claude to a CRM over MCP?

In Private CRM you create an API token on the API & MCP page, then add the server to Claude Code with claude mcp add --transport http and the /api/mcp URL, passing the token as a Bearer Authorization header. The token acts as the user who created it and sees the same records that user sees in the browser.

How do you stop an AI agent from making changes it shouldn't?

Private CRM uses several layers. New tokens are read-only and don't even list the write tools. Each token is limited to 120 calls a minute. No tool deletes or archives anything. And moving a deal to Won or Lost creates a pending change that a person approves or rejects on the deal page.

What does "idempotent" mean for an MCP tool?

Calling it twice has the same effect as calling it once. In Private CRM, create_contact with an email that exists returns the existing contact with created: false, log_activity with a Gmail message id already on the deal returns the first entry, and create_service updates the item with that SKU instead of adding a duplicate.

Does an MCP server need server-sent events?

Not if it never sends messages on its own. Private CRM answers POST requests with JSON-RPC replies and returns 405 Method Not Allowed on GET, since it has no server-initiated messages and so no event stream to open.

Written by

Abdulkader Safi

Software Engineer

Lead engineer at DSRPT, from Lebanon and based in Kuwait. I write about the tools and bugs from real client work, with the numbers I measured.

About me → GitHub LinkedIn

Need this kind of work done on your project?

Start a project →

Keep reading

More from Private CRM: an AI CRM where Claude does the data entry and you make the calls

All 12 entries →