Skip to content

Commit 2aede67

Browse files
jdevalkjanw-medavidmosterd
authored
Add file and url tools (#31)
* Rough create post command * intermediate commit * Move functions * Works? * Clean up * loop on methods * Added a smaller whitelist * stuff * remove param * DI * Better schema * whitespace * extract tool, use sanitize_key * naming, comments and scoping * First workign prototype * merge * WIP * allowed list naming * make the routes a list that is configuratble open/closed * Add required * small markup * Added some user routes * small todo * Make named variables in REST requests work * Added better debug output handling. * Moved to separate method * clean * make it work with multiple named variables * multiple matches in routes * Remove unneeded function * Ignore vscode folder * return * Dynamicly created tool descriptions. * Ignore the home route. * Housekeeping * Route information * Add a temporary limit on the number of tools * Add debug around skipping tools for registration * Better response output. * route information * cleanup * Add URL and file handling tools, in separate files --------- Co-authored-by: Jan-Willem Oostendorp <webmaster@janw.me> Co-authored-by: David Mosterd <david@codepress.nl>
1 parent 0fb4c0e commit 2aede67

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed

src/AiCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ private function register_tools( $server, $client ) {
9090
$map_rest_to_mcp = new MapRESTtoMCP();
9191
$map_rest_to_mcp->map_rest_to_mcp( $server );
9292

93+
new FileTools( $server );
94+
new URLTools( $server );
95+
9396
$server->register_tool(
9497
[
9598
'name' => 'calculate_total',

src/tools/file-tools.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
namespace AiCommand\Tools;
4+
5+
class FileTools {
6+
7+
private $server;
8+
9+
public function __construct( $server ) {
10+
$this->server = $server;
11+
$this->register_tools();
12+
}
13+
14+
public function register_tools() {
15+
$this->server->register_tool(
16+
[
17+
'name' => 'write_file',
18+
'description' => 'Writes a file.',
19+
'inputSchema' => [
20+
'type' => 'object',
21+
'properties' => [
22+
'path' => [
23+
'type' => 'string',
24+
'description' => 'The path of the file to write.',
25+
],
26+
'content' => [
27+
'type' => 'string',
28+
'description' => 'The content of the file to write.',
29+
],
30+
],
31+
'required' => [ 'path', 'content' ],
32+
],
33+
'callable' => function ( $params ) {
34+
$path = $params['path'];
35+
$content = $params['content'];
36+
return file_put_contents( $path, $content );
37+
},
38+
]
39+
);
40+
41+
$this->server->register_tool(
42+
[
43+
'name' => 'delete_file',
44+
'description' => 'Deletes a file.',
45+
'inputSchema' => [
46+
'type' => 'object',
47+
'properties' => [
48+
'path' => [
49+
'type' => 'string',
50+
'description' => 'The path of the file to delete.',
51+
],
52+
],
53+
'required' => [ 'path' ],
54+
],
55+
'callable' => function ( $params ) {
56+
$path = $params['path'];
57+
return unlink( $path );
58+
},
59+
]
60+
);
61+
62+
$this->server->register_tool(
63+
[
64+
'name' => 'read_file',
65+
'description' => 'Reads a file.',
66+
'inputSchema' => [
67+
'type' => 'object',
68+
'properties' => [
69+
'path' => [
70+
'type' => 'string',
71+
'description' => 'The path of the file to read.',
72+
],
73+
],
74+
'required' => [ 'path' ],
75+
],
76+
'callable' => function ( $params ) {
77+
$path = $params['path'];
78+
return file_get_contents( $path );
79+
},
80+
]
81+
);
82+
83+
$this->server->register_tool(
84+
[
85+
'name' => 'move_file',
86+
'description' => 'Moves a file.',
87+
'inputSchema' => [
88+
'type' => 'object',
89+
'properties' => [
90+
'path' => [
91+
'type' => 'string',
92+
'description' => 'The path of the file to move.',
93+
],
94+
'new_path' => [
95+
'type' => 'string',
96+
'description' => 'The new path of the file.',
97+
],
98+
],
99+
'required' => [ 'path', 'new_path' ],
100+
],
101+
'callable' => function ( $params ) {
102+
$path = $params['path'];
103+
$new_path = $params['new_path'];
104+
return rename( $path, $new_path );
105+
},
106+
]
107+
);
108+
109+
$this->server->register_tool(
110+
[
111+
'name' => 'list_files',
112+
'description' => 'Lists files in a directory.',
113+
'inputSchema' => [
114+
'type' => 'object',
115+
'properties' => [
116+
'path' => [
117+
'type' => 'string',
118+
'description' => 'The path of the directory to list files from.',
119+
],
120+
],
121+
'required' => [ 'path' ],
122+
],
123+
'callable' => function ( $params ) {
124+
$path = $params['path'];
125+
return scandir( $path );
126+
},
127+
]
128+
);
129+
130+
}
131+
}
132+

src/tools/url-tools.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace AiCommand\Tools;
4+
5+
class URLTools {
6+
7+
private $server;
8+
9+
public function __construct( $server ) {
10+
$this->server = $server;
11+
$this->register_tools();
12+
}
13+
14+
public function register_tools() {
15+
$this->server->register_tool(
16+
[
17+
'name' => 'retrieve_page',
18+
'description' => 'Retrieves a page from the web.',
19+
'inputSchema' => [
20+
'type' => 'object',
21+
'properties' => [
22+
'url' => [
23+
'type' => 'string',
24+
'description' => 'The URL of the page to retrieve.',
25+
],
26+
],
27+
'required' => [ 'url' ],
28+
],
29+
'callable' => function ( $params ) {
30+
$url = $params['url'];
31+
$response = wp_remote_get( $url );
32+
$body = wp_remote_retrieve_body( $response );
33+
return $body;
34+
},
35+
]
36+
);
37+
38+
$this->server->register_tool(
39+
[
40+
'name' => 'retrieve_rss_feed',
41+
'description' => 'Retrieves an RSS feed.',
42+
'inputSchema' => [
43+
'type' => 'object',
44+
'properties' => [
45+
'url' => [
46+
'type' => 'string',
47+
'description' => 'The URL of the RSS feed to retrieve.',
48+
],
49+
],
50+
'required' => [ 'url' ],
51+
],
52+
'callable' => function ( $params ) {
53+
$url = $params['url'];
54+
$response = wp_remote_get( $url );
55+
$body = wp_remote_retrieve_body( $response );
56+
$rss = simplexml_load_string( $body );
57+
return $rss;
58+
},
59+
]
60+
);
61+
}
62+
}
63+

0 commit comments

Comments
 (0)