This repository was archived by the owner on Oct 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.php
More file actions
executable file
·177 lines (151 loc) · 6.11 KB
/
server.php
File metadata and controls
executable file
·177 lines (151 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env php
<?php
/**
* WordPress MCP Server - Stdio Transport
*
* This file provides stdio transport for use with Claude Desktop and other MCP clients.
* For HTTP transport, use index.php instead.
*/
// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', '0');
ini_set('log_errors', '1');
ini_set('error_log', __DIR__ . '/php-errors.log');
// Custom error handler to capture all errors
set_error_handler(function($errno, $errstr, $errfile, $errline) {
$errorLog = __DIR__ . '/php-errors.log';
$message = date('[Y-m-d H:i:s] ') . "PHP Error [$errno]: $errstr in $errfile on line $errline\n";
file_put_contents($errorLog, $message, FILE_APPEND);
return false;
});
// Custom exception handler
set_exception_handler(function($exception) {
$errorLog = __DIR__ . '/php-errors.log';
$message = date('[Y-m-d H:i:s] ') . "Uncaught Exception: " . $exception->getMessage() . "\n";
$message .= "File: " . $exception->getFile() . " Line: " . $exception->getLine() . "\n";
$message .= "Trace: " . $exception->getTraceAsString() . "\n\n";
file_put_contents($errorLog, $message, FILE_APPEND);
});
require_once __DIR__ . '/vendor/autoload.php';
// Load WordPress using centralized loader
// This reads config.php and loads WordPress from the configured path
require_once __DIR__ . '/load-wordpress.php';
use Mcp\Server;
use Mcp\Server\Transport\StdioTransport;
// Import our services and tools
use InstaWP\MCP\PHP\Services\ValidationService;
use InstaWP\MCP\PHP\Services\WordPressService;
// Content tools
use InstaWP\MCP\PHP\Tools\Content\ListContent;
use InstaWP\MCP\PHP\Tools\Content\GetContent;
use InstaWP\MCP\PHP\Tools\Content\CreateContent;
use InstaWP\MCP\PHP\Tools\Content\UpdateContent;
use InstaWP\MCP\PHP\Tools\Content\DeleteContent;
use InstaWP\MCP\PHP\Tools\Content\DiscoverContentTypes;
use InstaWP\MCP\PHP\Tools\Content\GetContentBySlug;
use InstaWP\MCP\PHP\Tools\Content\FindContentByUrl;
// Taxonomy tools
use InstaWP\MCP\PHP\Tools\Taxonomy\DiscoverTaxonomies;
use InstaWP\MCP\PHP\Tools\Taxonomy\GetTaxonomy;
use InstaWP\MCP\PHP\Tools\Taxonomy\ListTerms;
use InstaWP\MCP\PHP\Tools\Taxonomy\GetTerm;
use InstaWP\MCP\PHP\Tools\Taxonomy\CreateTerm;
use InstaWP\MCP\PHP\Tools\Taxonomy\UpdateTerm;
use InstaWP\MCP\PHP\Tools\Taxonomy\DeleteTerm;
use InstaWP\MCP\PHP\Tools\Taxonomy\AssignTermsToContent;
use InstaWP\MCP\PHP\Tools\Taxonomy\GetContentTerms;
// Initialize services
$wpService = new WordPressService();
$validationService = new ValidationService();
// Initialize all tools
$tools = [
// Content tools
new ListContent($wpService, $validationService),
new GetContent($wpService, $validationService),
new CreateContent($wpService, $validationService),
new UpdateContent($wpService, $validationService),
new DeleteContent($wpService, $validationService),
new DiscoverContentTypes($wpService, $validationService),
new GetContentBySlug($wpService, $validationService),
new FindContentByUrl($wpService, $validationService),
// Taxonomy tools
new DiscoverTaxonomies($wpService, $validationService),
new GetTaxonomy($wpService, $validationService),
new ListTerms($wpService, $validationService),
new GetTerm($wpService, $validationService),
new CreateTerm($wpService, $validationService),
new UpdateTerm($wpService, $validationService),
new DeleteTerm($wpService, $validationService),
new AssignTermsToContent($wpService, $validationService),
new GetContentTerms($wpService, $validationService),
];
// Build server info description with capabilities
$capabilities = [
'safe_mode' => defined('WP_MCP_SAFE_MODE') ? WP_MCP_SAFE_MODE : false
];
$description = 'WordPress MCP Server (Stdio) - Capabilities: ' . json_encode($capabilities);
// Create the MCP server
$serverBuilder = Server::builder()
->setServerInfo('WordPress MCP Server (Stdio)', '1.0.0', $description);
// Register all tools with dynamic parameter handling
foreach ($tools as $tool) {
// Create a dynamic callable that accepts any parameters and passes them as an array to execute()
// The SDK will use reflection to map parameters from inputSchema to this function's parameters
// We dynamically create a function with the right signature based on the tool's schema
$schema = $tool->getSchema();
$params = [];
$paramNames = [];
foreach ($schema as $paramName => $rules) {
// All parameters are optional with null default, we'll handle validation in the tool
$params[] = "\${$paramName} = null";
$paramNames[] = "'{$paramName}'";
}
$paramSignature = implode(', ', $params);
$compactParams = implode(', ', $paramNames);
// Create a wrapper function with proper named parameters
if (empty($paramNames)) {
// Tool has no parameters
$wrapper = function() use ($tool) {
$result = $tool->execute([]);
return $result['data'] ?? $result;
};
} else {
$code = <<<PHP
return function({$paramSignature}) use (\$tool) {
\$params = array_filter(compact({$compactParams}), fn(\$v) => \$v !== null);
\$result = \$tool->execute(\$params);
return \$result['data'] ?? \$result;
};
PHP;
$wrapper = eval($code);
}
$serverBuilder->addTool(
$wrapper,
name: $tool->getName(),
description: $tool->getDescription(),
inputSchema: $tool->getInputSchema()
);
}
// Add site info resource
$serverBuilder->addResource(
function (): array {
return [
'site_name' => get_bloginfo('name'),
'site_url' => get_bloginfo('url'),
'admin_email' => get_bloginfo('admin_email'),
'wordpress_version' => get_bloginfo('version'),
'posts_count' => wp_count_posts('post')->publish,
'pages_count' => wp_count_posts('page')->publish,
];
},
uri: 'wordpress://site/info',
name: 'site_info',
description: 'WordPress site information and statistics',
mimeType: 'application/json'
);
$server = $serverBuilder->build();
// Create Stdio transport
$transport = new StdioTransport();
// Run the server
$result = $server->run($transport);
exit($result);