import React, { useEffect, useState } from 'react';
import { Notice } from './McpUi';
import { toast } from 'react-toastify';
import { mcpApi, t } from './api';
type Props = {
status: any;
onChange: () => void;
};
export const expiryLabel = ( days: number ): string => {
if ( 0 === days ) {
return t( 'mcp-expiry-never', 'Never' );
}
return `${ days } ${ t( 'mcp-days', 'days' ) }`;
};
/**
* Section 3: Credentials (strict mode, default expiry) and policies.
*/
const McpSecurity = ( { status, onChange }: Props ) => {
const [ form, setForm ] = useState( status.settings );
const [ saving, setSaving ] = useState( false );
useEffect( () => setForm( status.settings ), [ status.settings ] );
const dirty = JSON.stringify( form ) !== JSON.stringify( status.settings );
const save = async () => {
setSaving( true );
try {
await mcpApi( 'settings', 'POST', {
strict: form.strict,
expiry_days: Number( form.expiry_days ),
publishing: form.publishing,
allow_deletes: form.allow_deletes,
data_reads: form.data_reads,
} );
toast.success( t( 'mcp-saved', 'MCP settings saved.' ) );
onChange();
} catch ( e: any ) {
toast.error( e.message );
} finally {
setSaving( false );
}
};
return (
{ t( 'mcp-security', 'Credentials and policies' ) }
{ ! status.app_passwords_available && (
{ t( 'mcp-app-passwords-off', 'Application Passwords are not available on this site, so credentials cannot be generated. WordPress needs HTTPS (or a local environment), and some security plugins turn them off.' ) }
) }
setForm( { ...form, strict: e.target.checked } ) } />
{ t( 'mcp-strict', 'Only accept credentials generated here (recommended)' ) }
{ t( 'mcp-strict-desc', 'Credentials generated in "Connect an AI client" work only on the MCP endpoint and expire. Other Application Passwords open the whole WordPress REST API, so they are refused while this is on.' ) }
{ t( 'mcp-default-expiry', 'Default credential expiry' ) }
setForm( { ...form, expiry_days: Number( e.target.value ) } ) }>
{ ( status.expiry_choices || [ 30, 90, 365, 0 ] ).map( ( d: number ) => (
{ expiryLabel( d ) }
) ) }
{ t( 'mcp-publishing', 'Pages and posts' ) }
setForm( { ...form, publishing: e.target.value } ) }>
{ t( 'mcp-publishing-drafts', 'Drafts only (recommended)' ) }
{ t( 'mcp-publishing-confirm', 'Allow publishing, with confirmation' ) }
{ t( 'mcp-publishing-off', 'Off: never change pages or posts' ) }
{ t( 'mcp-publishing-desc', 'With "Drafts only", AI clients can create draft pages and edit drafts, but never publish or change live content.' ) }
setForm( { ...form, allow_deletes: e.target.checked } ) } />
{ t( 'mcp-allow-deletes', 'Allow deleting tables and tab groups' ) }
{ t( 'mcp-allow-deletes-desc', 'Deletes always need a preview and a confirmation from you in the AI chat.' ) }
setForm( { ...form, data_reads: e.target.checked } ) } />
{ t( 'mcp-data-reads', 'Allow reading table data' ) }
{ t( 'mcp-data-reads-desc', 'Lets AI clients read the rows visitors can see, to answer questions or write summaries. Hidden rows, columns and cells are never shared.' ) }
{ saving ? t( 'mcp-saving', 'Saving…' ) : t( 'mcp-save', 'Save changes' ) }
);
};
export default McpSecurity;