# betterdocs/4.9.5/react-src/admin/mcp/utils/api.js

BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ &amp; Chatbot, version 4.9.5. 51 lines.

- Page: https://pluginprobe.com/plugins/betterdocs/4.9.5/code/react-src/admin/mcp/utils/api.js
- Raw: https://pluginprobe.com/plugins/betterdocs/4.9.5/raw/react-src/admin/mcp/utils/api.js
- Modified: 2026-10-12T10:07:14+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/betterdocs/4.9.5/code/react-src/admin/mcp/utils/api.js#L10-L20`.

```javascript
import apiFetch from "@wordpress/api-fetch";

/**
 * Every call on this page goes through one place, so the nonce is attached
 * exactly once per request and the route prefix is written once. The MCP
 * management routes are `manage_options`-gated cookie-authenticated REST
 * routes — without the nonce WordPress treats the request as logged out and
 * answers 401, which is the failure mode this wrapper exists to prevent.
 */
const NS = () => window.betterdocs_admin?.mcp?.rest || "betterdocs/v1";

function withNonce() {
    apiFetch.use(apiFetch.createNonceMiddleware(window.betterdocs_admin?.nonce));
}

function request(path, options = {}) {
    withNonce();
    return apiFetch({ path: `${NS()}/${path}`, ...options });
}

export const getConnection = () => request("mcp/connection");
export const connect = (readOnly = false) =>
    request("mcp/connect", { method: "POST", data: { read_only: !!readOnly } });
export const rotate = (readOnly = null) =>
    request("mcp/rotate", {
        method: "POST",
        data: readOnly === null ? {} : { read_only: !!readOnly },
    });
export const disconnect = () => request("mcp/disconnect", { method: "POST" });

export const getApps = () => request("mcp/apps");
export const revokeApp = (clientId) =>
    request("mcp/apps/revoke", { method: "POST", data: { client_id: clientId } });

export const getHealth = () => request("mcp/health");
export const runSelfTest = () => request("mcp/self-test", { method: "POST" });

/**
 * The master switch writes through BetterDocs' own settings route, the same
 * one the Settings screen uses, so the value is normalised by `Settings` and
 * every other consumer sees it immediately.
 */
export const saveEnableMcp = (enabled) => {
    withNonce();
    return apiFetch({
        path: `${NS()}/settings`,
        method: "POST",
        data: { enable_mcp: !!enabled },
    });
};

```
