/mcp/. This class: * * - learns whether it is on by asking that endpoint (a JSON-RPC ping: 200 on, 404 off), at most once an hour (a transient); * - when on, serves /llms.txt (the shop's name and description, the endpoint, the main categories and the policy pages) and adds * to the shop's ; * - always answers GET /wp-json/codexta/v1/shop-info: the same public facts as JSON (read by llms.txt). */ class Codexta_Agent_Ready { const TRANSIENT = 'codexta_agent_ready'; /** * Register the hooks. */ public static function init() { add_action( 'rest_api_init', array( __CLASS__, 'routes' ) ); add_action( 'parse_request', array( __CLASS__, 'llms_txt' ), 1 ); // After init: WooCommerce registers its categories on init. add_action( 'wp_head', array( __CLASS__, 'head' ), 2 ); } /** * The endpoint's public address. * * @return string */ public static function mcp_url() { return Codexta_Connect::base_url() . '/mcp/' . Codexta_Connect::site_uuid(); } /** * Whether Codexta serves the endpoint (asked at most once an hour; an unreachable Codexta counts as off for 5 minutes). * * @return bool */ public static function on() { if ( ! Codexta_Connect::is_connected() ) { return false; } $cached = get_transient( self::TRANSIENT ); if ( false !== $cached ) { return 'on' === $cached; } $response = wp_remote_post( Codexta_Connect::server_url() . '/mcp/' . Codexta_Connect::site_uuid(), array( 'timeout' => 3, 'redirection' => 0, 'headers' => array( 'Content-Type' => 'application/json', 'Accept' => 'application/json, text/event-stream', ), 'body' => wp_json_encode( array( 'jsonrpc' => '2.0', 'id' => 1, 'method' => 'ping', ) ), ) ); $code = is_wp_error( $response ) ? 0 : (int) wp_remote_retrieve_response_code( $response ); $on = 200 === $code; set_transient( self::TRANSIENT, $on ? 'on' : 'off', ( 200 === $code || 404 === $code ) ? HOUR_IN_SECONDS : 5 * MINUTE_IN_SECONDS ); return $on; } /** * GET /wp-json/codexta/v1/shop-info. */ public static function routes() { register_rest_route( 'codexta/v1', '/shop-info', array( 'methods' => 'GET', 'permission_callback' => '__return_true', // Public: what any visitor of the shop can read. 'callback' => array( __CLASS__, 'info' ), ) ); } /** * The shop's public facts: name, description, address, currency, language, main categories and policy pages. * * @return array */ public static function info() { $pages = array(); $ids = array( 'returns' => (int) get_option( 'woocommerce_refund_returns_page_id' ), 'terms' => function_exists( 'wc_terms_and_conditions_page_id' ) ? (int) wc_terms_and_conditions_page_id() : 0, 'privacy' => (int) get_option( 'wp_page_for_privacy_policy' ), ); foreach ( $ids as $kind => $id ) { if ( $id > 0 && 'publish' === get_post_status( $id ) ) { $pages[ $kind ] = get_permalink( $id ); } } $categories = array(); $default_category_id = (int) get_option( 'default_product_cat' ); $terms = get_terms( array( 'taxonomy' => 'product_cat', 'parent' => 0, 'hide_empty' => true, 'number' => 30, ) ); foreach ( is_array( $terms ) ? $terms : array() as $term ) { $link = get_term_link( $term ); if ( (int) $term->term_id === $default_category_id || is_wp_error( $link ) ) { continue; } $categories[] = array( 'name' => html_entity_decode( $term->name, ENT_QUOTES, 'UTF-8' ), 'slug' => $term->slug, 'url' => $link, ); } return array( 'name' => html_entity_decode( get_bloginfo( 'name' ), ENT_QUOTES, 'UTF-8' ), 'description' => html_entity_decode( get_bloginfo( 'description' ), ENT_QUOTES, 'UTF-8' ), 'url' => home_url( '/' ), 'currency' => function_exists( 'get_woocommerce_currency' ) ? get_woocommerce_currency() : '', 'language' => str_replace( '_', '-', get_locale() ), 'categories' => $categories, 'pages' => (object) $pages, ); } /** * /llms.txt, when the endpoint is on (otherwise WordPress answers its usual 404). Matched on the exact path, no rewrite rule. */ public static function llms_txt() { $path = isset( $_SERVER['REQUEST_URI'] ) ? (string) wp_parse_url( esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ), PHP_URL_PATH ) : ''; $want = (string) wp_parse_url( home_url( '/llms.txt' ), PHP_URL_PATH ); if ( $path !== $want || ! self::on() ) { return; } // Every value is escaped as it goes in (esc_html for text, esc_url for links); the Markdown around them is literal. $i = self::info(); $lines_escaped = array( '# ' . esc_html( $i['name'] ), '' ); if ( '' !== $i['description'] ) { $lines_escaped[] = '> ' . esc_html( $i['description'] ); $lines_escaped[] = ''; } $lines_escaped[] = 'Online shop at ' . esc_url( $i['url'] ) . ' (prices in ' . esc_html( $i['currency'] ) . ', language ' . esc_html( $i['language'] ) . '). AI shopping agents can search the catalogue, read products and build a cart link through MCP (no customer data).'; $lines_escaped[] = ''; $lines_escaped[] = '## MCP'; $lines_escaped[] = '- [MCP endpoint (Streamable HTTP)](' . esc_url( self::mcp_url() ) . '): the shop\'s product and cart-link tools (tools/list names them)'; if ( ! empty( $i['categories'] ) ) { $lines_escaped[] = ''; $lines_escaped[] = '## Categories'; foreach ( $i['categories'] as $c ) { $lines_escaped[] = '- [' . esc_html( $c['name'] ) . '](' . esc_url( $c['url'] ) . ')'; } } $pages = (array) $i['pages']; if ( ! empty( $pages ) ) { $names = array( 'returns' => 'Returns and refunds', 'terms' => 'Terms and conditions', 'privacy' => 'Privacy policy', ); $lines_escaped[] = ''; $lines_escaped[] = '## Policies'; foreach ( $pages as $kind => $url ) { $lines_escaped[] = '- [' . esc_html( $names[ $kind ] ) . '](' . esc_url( $url ) . ')'; } } status_header( 200 ); header( 'Content-Type: text/plain; charset=utf-8' ); header( 'X-Robots-Tag: noindex' ); header( 'X-Content-Type-Options: nosniff' ); echo implode( "\n", $lines_escaped ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- every value escaped above. exit; } /** * in the shop's head, when the endpoint is on. */ public static function head() { if ( self::on() ) { echo '' . "\n"; } } }