VOOZH about

URL: https://deepwiki.com/WordPress/mcp-adapter/2.3-basic-examples

⇱ Basic Examples | WordPress/mcp-adapter | DeepWiki


Loading...
Menu

Basic Examples

This page provides practical, working examples for creating and testing MCP tools, resources, and prompts using the WordPress MCP Adapter. Each example demonstrates the minimum required configuration to expose WordPress abilities through the Model Context Protocol.

For comprehensive details on ability creation, schema design, and advanced features, see Creating Abilities. For setting up your development environment, see Development Environment. For information about the architecture and request flow, see Architecture.

Default MCP Server

The MCP Adapter automatically creates a default server that exposes all abilities marked with meta.mcp.public = true. This server is immediately available once the plugin is active.

PropertyValue
Server IDmcp-adapter-default-server
REST Endpoint/wp-json/mcp/mcp-adapter-default-server
Supported TransportsHTTP, STDIO (via WP-CLI)
Ability FilterAll abilities with meta.mcp.public = true
Default Permission Callbackis_user_logged_in()

The default server is registered automatically by includes/mcp-adapter.php40-57 during the mcp_adapter_init action. Custom servers can be created using the McpAdapter::create_server() method as documented in Server Configuration.

Sources: docs/getting-started/basic-examples.md245-250 includes/mcp-adapter.php40-57

Example 1: Tool - Create Post

Tools execute actions and return results. The following example creates a tool that inserts WordPress posts with validation and permission checks.


Registration Code

The ability is registered using wp_register_ability() during the wp_abilities_api_init action:

docs/getting-started/basic-examples.md12-106

Key configuration elements:

FieldPurpose
input_schemaDefines required fields (title, content) and optional fields (status, category) with validation constraints
output_schemaSpecifies the structure of the returned data (post_id, post_url, edit_url)
execute_callbackImplements the post creation logic using wp_insert_post()
permission_callbackRestricts access to users with publish_posts capability
meta.mcp.publicExposes the ability through the default MCP server
meta.annotationsProvides behavior hints (readOnlyHint, destructiveHint)

Schema Transformation

The input schema is automatically validated against the ability definition before execution. The output schema is used by MCP clients to understand the response structure. No transformation is needed for object schemas - they pass through unchanged as shown in includes/Domain/Utils/SchemaTransformer.php56-62

Testing the Tool


Sources: docs/getting-started/basic-examples.md5-117 includes/Domain/Utils/SchemaTransformer.php20-92

Example 2: Resource - Site Configuration

Resources provide read-only access to data. They require a uri in the ability metadata and should set type: 'resource' for proper discovery.


Registration Code

docs/getting-started/basic-examples.md124-157

Key configuration elements:

FieldPurpose
meta.uriRequired for resources. Unique identifier (e.g., wordpress://site/config)
meta.mcp.typeSet to 'resource' for proper auto-discovery via resources/list
execute_callbackReturns structured data without modifying state
permission_callbackRestricts access to administrators (manage_options)
meta.annotations.audienceIndicates intended consumers (user, assistant)
meta.annotations.priorityRelative importance (0.0 to 1.0) for resource ranking

Resources are identified by their URI during resources/read requests. The ResourcesHandler includes/Transport/Handlers/ResourcesHandler.php looks up resources by URI and executes the underlying ability.

Testing the Resource


Sources: docs/getting-started/basic-examples.md119-169 docs/guides/creating-abilities.md544-577

Example 3: Prompt - Code Review

Prompts generate structured messages for language models. They use meta.arguments or input_schema to define parameters and must return a messages array.


Registration Code

docs/getting-started/basic-examples.md178-229

Key configuration elements:

FieldPurpose
meta.argumentsDefines prompt parameters (name, description, required) in MCP format
meta.mcp.typeSet to 'prompt' for auto-discovery via prompts/list
execute_callbackReturns messages array with role and content fields
content.annotationsMessage-level metadata for specific content blocks
meta.annotationsTemplate-level metadata describing the prompt's characteristics

Alternative: Using input_schema

Instead of meta.arguments, prompts can define parameters using input_schema, which is automatically converted to MCP argument format by includes/Domain/Conversions/RegisterAbilityAsMcpPrompt.php This approach provides JSON Schema validation:


See docs/guides/creating-abilities.md586-601 for the complete example using input_schema.

Testing the Prompt


Sources: docs/getting-started/basic-examples.md172-241 docs/guides/creating-abilities.md580-655

Testing Workflow

The following diagram illustrates the complete testing workflow using WP-CLI's STDIO transport:


Common Test Commands


The --user=admin flag sets the current user context for permission checks. The --server=mcp-adapter-default-server flag specifies which MCP server to use. For custom servers, replace with your server ID.

Sources: docs/getting-started/basic-examples.md261-271 docs/troubleshooting/common-issues.md28-32

Component Type Comparison

The following table summarizes the key differences between the three MCP component types:

AspectToolsResourcesPrompts
MCP Methodtools/callresources/readprompts/get
Discovery Methodtools/listresources/listprompts/list
Required Metadatameta.mcp.public = truemeta.uri (required)
meta.mcp.type = 'resource'
meta.arguments or input_schema
meta.mcp.type = 'prompt'
Return ValueArbitrary structured dataArbitrary structured data{messages: [...]} array
Annotations TypeToolAnnotations
(readOnlyHint, destructiveHint, idempotentHint)
Annotations
(audience, priority, lastModified)
Annotations
(audience, priority, lastModified)
Typical Use CaseExecute actions, modify dataProvide read-only data accessGenerate prompts for LLMs
Schema SupportObject or flattenedN/A (no input schema)Arguments converted from input_schema

Sources: docs/getting-started/basic-examples.md244-259 docs/guides/creating-abilities.md312-363

Ability Exposure Flow

The following diagram shows how abilities are registered, filtered, and exposed through the default MCP server:


Key Points:

  1. Registration: Abilities are registered using wp_register_ability() during the wp_abilities_api_init action
  2. Filtering: Only abilities with meta.mcp.public = true are exposed through the default server
  3. Type Detection: The meta.mcp.type field determines which converter is used (tool, resource, or prompt)
  4. Default Type: If meta.mcp.type is not specified, abilities default to type: 'tool'
  5. Exposure: All converted components are available immediately through the REST endpoint

Sources: includes/mcp-adapter.php40-57 docs/guides/creating-abilities.md14-36

Schema Transformation for Flattened Schemas

For simple abilities that accept or return single values, the MCP Adapter supports flattened schemas that are automatically transformed to MCP-compatible object format:


The SchemaTransformer class includes/Domain/Utils/SchemaTransformer.php20-92 handles this transformation automatically. Key behaviors:

  1. Object schemas pass through unchanged includes/Domain/Utils/SchemaTransformer.php56-62
  2. Primitive types (string, number, integer, boolean, array) are wrapped includes/Domain/Utils/SchemaTransformer.php64-70
  3. Input schemas use "input" as the wrapper property
  4. Output schemas use "result" as the wrapper property (see docs/guides/creating-abilities.md174-194)
  5. All metadata (descriptions, constraints, enums) is preserved during transformation tests/Unit/Domain/Utils/SchemaTransformerTest.php186-204

For detailed information on flattened vs object schemas, see Creating Abilities and Schema Transformation.

Sources: includes/Domain/Utils/SchemaTransformer.php20-92 docs/guides/creating-abilities.md84-144 tests/Unit/Domain/Utils/SchemaTransformerTest.php20-313

Next Steps

After completing these basic examples, explore:

For troubleshooting common issues, see Troubleshooting Guide.

Sources: docs/getting-started/basic-examples.md273-278