Skip to content

Commit 9cf57e3

Browse files
committed
fix: Fixed bugs:
- Added missed attributes for some tools - Fixed path to a config file for MCP server - Fixed cli commands SelfUpdate and Version
1 parent 792f001 commit 9cf57e3

13 files changed

+86
-160
lines changed

context.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,3 @@ documents:
273273
sources:
274274
- type: git_diff
275275
commit: unstaged
276-

src/ConfigLoader/ConfigLoaderFactory.php

+13-13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Butschster\ContextGenerator\ConfigLoader\Reader\PhpReader;
1515
use Butschster\ContextGenerator\ConfigLoader\Reader\StringJsonReader;
1616
use Butschster\ContextGenerator\ConfigLoader\Reader\YamlReader;
17+
use Butschster\ContextGenerator\Directories;
1718
use Butschster\ContextGenerator\FilesInterface;
1819
use Butschster\ContextGenerator\Lib\Logger\HasPrefixLoggerInterface;
1920
use Psr\Log\LoggerInterface;
@@ -25,18 +26,17 @@
2526
{
2627
public function __construct(
2728
private FilesInterface $files,
28-
private string $rootPath,
29+
private Directories $dirs,
2930
private ?LoggerInterface $logger = null,
3031
) {}
3132

3233
/**
3334
* Create a loader for a specific config file
3435
*
35-
* @param string $rootPath Full path to the config file
3636
* @param array<ConfigParserPluginInterface> $parserPlugins Plugins for the config parser
3737
* @return ConfigLoaderInterface The config loader
3838
*/
39-
public function create(string $rootPath, array $parserPlugins = []): ConfigLoaderInterface
39+
public function create(Directories $dirs, array $parserPlugins = []): ConfigLoaderInterface
4040
{
4141
\assert($this->logger instanceof HasPrefixLoggerInterface);
4242

@@ -57,7 +57,7 @@ public function create(string $rootPath, array $parserPlugins = []): ConfigLoade
5757
$parserPlugins = [$importParserPlugin, ...$parserPlugins];
5858

5959
// Create parser
60-
$parser = new ConfigParser($this->rootPath, $this->logger, ...$parserPlugins);
60+
$parser = new ConfigParser($dirs->configPath, $this->logger, ...$parserPlugins);
6161

6262
// Create composite parser
6363
$compositeParser = new CompositeConfigParser($parser);
@@ -80,28 +80,28 @@ public function create(string $rootPath, array $parserPlugins = []): ConfigLoade
8080

8181
// Try different file extensions
8282
$jsonLoader = new ConfigLoader(
83-
configPath: $rootPath . '/context.json',
83+
configPath: $dirs->getConfigPath('context.json'),
8484
reader: $jsonReader,
8585
parser: $compositeParser,
8686
logger: $this->logger,
8787
);
8888

8989
$yamlLoader = new ConfigLoader(
90-
configPath: $rootPath . '/context.yaml',
90+
configPath: $dirs->getConfigPath('context.yaml'),
9191
reader: $yamlReader,
9292
parser: $compositeParser,
9393
logger: $this->logger,
9494
);
9595

9696
$ymlLoader = new ConfigLoader(
97-
configPath: $rootPath . '/context.yml',
97+
configPath: $dirs->getConfigPath('context.yml'),
9898
reader: $yamlReader,
9999
parser: $compositeParser,
100100
logger: $this->logger,
101101
);
102102

103103
$phpLoader = new ConfigLoader(
104-
configPath: $rootPath . '/context.php',
104+
configPath: $dirs->getConfigPath('context.php'),
105105
reader: $phpReader,
106106
parser: $compositeParser,
107107
logger: $this->logger,
@@ -115,7 +115,7 @@ public function create(string $rootPath, array $parserPlugins = []): ConfigLoade
115115
}
116116

117117
// Add a new method to ConfigLoaderFactory to create a loader for a specific file path
118-
public function createForFile(string $filePath, array $parserPlugins = []): ConfigLoaderInterface
118+
public function createForFile(Directories $dirs, array $parserPlugins = []): ConfigLoaderInterface
119119
{
120120
\assert($this->logger instanceof HasPrefixLoggerInterface);
121121

@@ -136,13 +136,13 @@ public function createForFile(string $filePath, array $parserPlugins = []): Conf
136136
$parserPlugins = [$importParserPlugin, ...$parserPlugins];
137137

138138
// Create parser
139-
$parser = new ConfigParser($this->rootPath, $this->logger, ...$parserPlugins);
139+
$parser = new ConfigParser($this->dirs->rootPath, $this->logger, ...$parserPlugins);
140140

141141
// Create composite parser
142142
$compositeParser = new CompositeConfigParser($parser);
143143

144144
// Determine the file extension
145-
$extension = \pathinfo($filePath, PATHINFO_EXTENSION);
145+
$extension = \pathinfo($dirs->configPath, PATHINFO_EXTENSION);
146146

147147
// Create the appropriate reader based on file extension
148148
$reader = match ($extension) {
@@ -163,7 +163,7 @@ public function createForFile(string $filePath, array $parserPlugins = []): Conf
163163

164164
// Create loader for the specific file
165165
return new ConfigLoader(
166-
configPath: $filePath,
166+
configPath: $dirs->configPath,
167167
reader: $reader,
168168
parser: $compositeParser,
169169
logger: $this->logger,
@@ -182,7 +182,7 @@ public function createFromString(string $jsonConfig, array $parserPlugins = []):
182182
\assert($this->logger instanceof HasPrefixLoggerInterface);
183183

184184
// Create parser
185-
$parser = new ConfigParser($this->rootPath, $this->logger, ...$parserPlugins);
185+
$parser = new ConfigParser($this->dirs->rootPath, $this->logger, ...$parserPlugins);
186186

187187
// Create composite parser
188188
$compositeParser = new CompositeConfigParser($parser);

src/ConfigLoader/ConfigurationProvider.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Butschster\ContextGenerator\ConfigLoader;
66

77
use Butschster\ContextGenerator\ConfigLoader\Exception\ConfigLoaderException;
8+
use Butschster\ContextGenerator\Directories;
89
use Butschster\ContextGenerator\FilesInterface;
910
use Psr\Log\LoggerInterface;
1011

@@ -16,7 +17,7 @@
1617
public function __construct(
1718
private ConfigLoaderFactory $loaderFactory,
1819
private FilesInterface $files,
19-
private string $rootPath,
20+
private Directories $dirs,
2021
private ?LoggerInterface $logger = null,
2122
private array $parserPlugins = [],
2223
) {}
@@ -47,7 +48,7 @@ public function fromPath(string $configPath): ConfigLoaderInterface
4748
]);
4849

4950
return $this->loaderFactory->create(
50-
rootPath: $resolvedPath,
51+
dirs: $this->dirs->withConfigPath($resolvedPath),
5152
parserPlugins: $this->parserPlugins,
5253
);
5354
}
@@ -56,10 +57,9 @@ public function fromPath(string $configPath): ConfigLoaderInterface
5657
]);
5758

5859
return $this->loaderFactory->createForFile(
59-
filePath: $resolvedPath,
60+
dirs: $this->dirs->withConfigPath($resolvedPath),
6061
parserPlugins: $this->parserPlugins,
6162
);
62-
6363
}
6464

6565
/**
@@ -68,11 +68,11 @@ public function fromPath(string $configPath): ConfigLoaderInterface
6868
public function fromDefaultLocation(): ConfigLoaderInterface
6969
{
7070
$this->logger?->info('Loading configuration from default location', [
71-
'rootPath' => $this->rootPath,
71+
'rootPath' => $this->dirs->rootPath,
7272
]);
7373

7474
return $this->loaderFactory->create(
75-
rootPath: $this->rootPath,
75+
dirs: $this->dirs,
7676
parserPlugins: $this->parserPlugins,
7777
);
7878
}
@@ -87,7 +87,7 @@ private function resolvePath(string $path): string
8787
$resolvedPath = $path;
8888
} else {
8989
// Otherwise, resolve it relative to the root path
90-
$resolvedPath = \rtrim($this->rootPath, '/') . '/' . $path;
90+
$resolvedPath = \rtrim($this->dirs->rootPath, '/') . '/' . $path;
9191
}
9292

9393
// Check if the path exists

src/ConfigurationProviderFactory.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public function create(Directories $dirs): ConfigurationProvider
2222
return new ConfigurationProvider(
2323
loaderFactory: new ConfigLoaderFactory(
2424
files: $this->files,
25-
rootPath: $dirs->rootPath,
25+
dirs: $dirs,
2626
logger: $this->logger->withPrefix('config-loader'),
2727
),
2828
files: $this->files,
29-
rootPath: $dirs->rootPath,
29+
dirs: $dirs,
3030
logger: $this->logger->withPrefix('config-provider'),
3131
parserPlugins: $this->parserPluginRegistry->getPlugins(),
3232
);

src/Console/MCPServerCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function __invoke(
8181
'path' => $dirs->rootPath,
8282
],
8383
);
84-
$loader = $configProvider->fromPath($configPath);
84+
$loader = $configProvider->fromPath($dirs->configPath);
8585
} else {
8686
$this->logger->info('Using default configuration location...');
8787
$loader = $configProvider->fromDefaultLocation();

src/Console/SelfUpdateCommand.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
use Spiral\Core\Container;
1111
use Symfony\Component\Console\Attribute\AsCommand;
1212
use Symfony\Component\Console\Command\Command;
13-
use Symfony\Component\Console\Input\InputInterface;
1413
use Symfony\Component\Console\Input\InputOption;
15-
use Symfony\Component\Console\Output\OutputInterface;
1614

1715
#[AsCommand(
1816
name: 'self-update',
@@ -43,7 +41,7 @@ public function __construct(
4341
parent::__construct($container);
4442
}
4543

46-
public function __invoke(InputInterface $input, OutputInterface $output): int
44+
public function __invoke(): int
4745
{
4846
$this->output->title('Context Generator Self Update');
4947

src/Console/VersionCommand.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
use Spiral\Core\Container;
1010
use Symfony\Component\Console\Attribute\AsCommand;
1111
use Symfony\Component\Console\Command\Command;
12-
use Symfony\Component\Console\Input\InputInterface;
1312
use Symfony\Component\Console\Input\InputOption;
14-
use Symfony\Component\Console\Output\OutputInterface;
1513

1614
#[AsCommand(
1715
name: 'version',
@@ -32,7 +30,7 @@ public function __construct(
3230
parent::__construct($container);
3331
}
3432

35-
public function __invoke(InputInterface $input, OutputInterface $output): int
33+
public function __invoke(): int
3634
{
3735
$this->output->title('Context Generator');
3836
$this->output->text('Current version: ' . $this->version);

src/Directories.php

+14-9
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ public function getFilePath(string $filename): string
6666
return \sprintf('%s/%s', $this->rootPath, $filename);
6767
}
6868

69+
public function getConfigPath(string $filename): string
70+
{
71+
$filename = \ltrim($filename, '/');
72+
73+
return \sprintf('%s/%s', $this->configPath, $filename);
74+
}
75+
6976
/**
7077
* Determine the effective root path based on config file path
7178
*/
@@ -75,17 +82,15 @@ public function determineRootPath(?string $configPath = null, ?string $inlineCon
7582
return $this;
7683
}
7784

78-
// If config path is absolute, use its directory as root
79-
if (\str_starts_with($configPath, '/')) {
80-
$configDir = \rtrim(\is_dir($configPath) ? $configPath : \dirname($configPath));
81-
} else {
82-
// If relative, resolve against the original root path
83-
$fullConfigPath = \rtrim($this->rootPath, '/') . '/' . $configPath;
84-
85-
$configDir = \rtrim(\is_dir($fullConfigPath) ? $fullConfigPath : \dirname($fullConfigPath));
85+
// If relative, resolve against the original root path
86+
if (!\str_starts_with($configPath, '/')) {
87+
$configPath = \rtrim($this->rootPath, '/') . '/' . $configPath;
8688
}
8789

88-
if (\file_exists($configDir) && \is_dir($configDir)) {
90+
// If config path is absolute, use its directory as root
91+
$configDir = \rtrim(\is_dir($configPath) ? $configPath : \dirname($configPath));
92+
93+
if (\is_dir($configDir)) {
8994
return $this->withRootPath($configDir)->withConfigPath($configPath);
9095
}
9196

src/McpServer/Action/Resources/ListResourcesAction.php

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function __invoke(ServerRequestInterface $request): ListResourcesResult
3434

3535
// Add document resources from config loader
3636
$documents = $this->configLoader->load();
37+
3738
foreach ($documents->getItems() as $document) {
3839
$resources[] = new Resource(
3940
name: $document->outputPath,

src/McpServer/Action/Tools/Context/ContextAction.php

+5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
namespace Butschster\ContextGenerator\McpServer\Action\Tools\Context;
66

77
use Butschster\ContextGenerator\ConfigLoader\ConfigLoaderInterface;
8+
use Butschster\ContextGenerator\McpServer\Attribute\Tool;
89
use Butschster\ContextGenerator\McpServer\Routing\Attribute\Post;
910
use Mcp\Types\CallToolResult;
1011
use Mcp\Types\TextContent;
1112
use Psr\Http\Message\ServerRequestInterface;
1213
use Psr\Log\LoggerInterface;
1314

15+
#[Tool(
16+
name: 'context',
17+
description: 'List all contexts in the project context config',
18+
)]
1419
final readonly class ContextAction
1520
{
1621
public function __construct(

src/McpServer/Action/Tools/Context/ContextGetAction.php

+12
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,24 @@
77
use Butschster\ContextGenerator\ConfigLoader\ConfigLoaderInterface;
88
use Butschster\ContextGenerator\Document\Compiler\DocumentCompiler;
99
use Butschster\ContextGenerator\Document\Compiler\Error\ErrorCollection;
10+
use Butschster\ContextGenerator\McpServer\Attribute\InputSchema;
11+
use Butschster\ContextGenerator\McpServer\Attribute\Tool;
1012
use Butschster\ContextGenerator\McpServer\Routing\Attribute\Post;
1113
use Mcp\Types\CallToolResult;
1214
use Mcp\Types\TextContent;
1315
use Psr\Http\Message\ServerRequestInterface;
1416
use Psr\Log\LoggerInterface;
1517

18+
#[Tool(
19+
name: 'context-get',
20+
description: 'Get a specific context document from the project context config',
21+
)]
22+
#[InputSchema(
23+
name: 'path',
24+
type: 'string',
25+
description: 'Output path to the context document provided in the config.',
26+
required: true,
27+
)]
1628
final readonly class ContextGetAction
1729
{
1830
public function __construct(

0 commit comments

Comments
 (0)