Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions src/Core/MCPServer.Logger.pas
Original file line number Diff line number Diff line change
Expand Up @@ -269,36 +269,58 @@ class function TLogger.GetOnLogMessage: TLogMessageProc;
end;

class procedure TLogger.SetLogToConsole(const Value: Boolean);
var
lInstance: TLogger;
begin
Instance.FLogToConsole := Value;
lInstance := Instance;
if Assigned(lInstance) then
lInstance.FLogToConsole := Value;
end;

class procedure TLogger.SetLogToFile(const Value: Boolean);
var
lInstance: TLogger;
begin
Instance.FLogToFile := Value;
lInstance := Instance;
if Assigned(lInstance) then
lInstance.FLogToFile := Value;
end;

class procedure TLogger.SetLogFileName(const Value: string);
var
lInstance: TLogger;
begin
FLock.Enter;
try
Instance.FLogFileName := Value;
lInstance := Instance;
if lInstance = nil then
Exit;

lInstance.FLogFileName := Value;

if Assigned(Instance.FLogFile) then
FreeAndNil(Instance.FLogFile);
if Assigned(lInstance.FLogFile) then
FreeAndNil(lInstance.FLogFile);
finally
FLock.Leave;
end;
end;

class procedure TLogger.SetMinLogLevel(const Value: TLogLevel);
var
lInstance: TLogger;
begin
Instance.FMinLogLevel := Value;
lInstance := Instance;
if Assigned(lInstance) then
lInstance.FMinLogLevel := Value;
end;

class procedure TLogger.SetOnLogMessage(const Value: TLogMessageProc);
var
lInstance: TLogger;
begin
Instance.FOnLogMessage := Value;
lInstance := Instance;
if Assigned(lInstance) then
lInstance.FOnLogMessage := Value;
end;

class function TLogger.GetUseStdErr: Boolean;
Expand All @@ -307,8 +329,12 @@ class function TLogger.GetUseStdErr: Boolean;
end;

class procedure TLogger.SetUseStdErr(const Value: Boolean);
var
lInstance: TLogger;
begin
Instance.FUseStdErr := Value;
lInstance := Instance;
if Assigned(lInstance) then
lInstance.FUseStdErr := Value;
end;

end.
4 changes: 3 additions & 1 deletion src/Core/MCPServer.ManagerRegistry.pas
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ procedure TMCPManagerRegistry.RegisterManager(const Manager: IMCPCapabilityManag
end;

function TMCPManagerRegistry.GetManagerForMethod(const Method: string): IMCPCapabilityManager;
var
Manager: IMCPCapabilityManager;
begin
Result := nil;
for var Manager in FManagers do
for Manager in FManagers do
begin
if Manager.HandlesMethod(Method) then
begin
Expand Down
18 changes: 12 additions & 6 deletions src/Core/MCPServer.Settings.pas
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ TMCPSettings = class
procedure LoadDefaults;
procedure CreateDefaultSettingsFile;
public
constructor Create(const ASettingsFile: string = '');
constructor Create(const ASettingsFile: string = ''; const ACreateFile: Boolean = True);
destructor Destroy; override;

procedure LoadFromFile;
Expand Down Expand Up @@ -55,7 +55,7 @@ implementation

{ TMCPSettings }

constructor TMCPSettings.Create(const ASettingsFile: string);
constructor TMCPSettings.Create(const ASettingsFile: string; const ACreateFile: Boolean);
begin
inherited Create;

Expand All @@ -66,7 +66,7 @@ constructor TMCPSettings.Create(const ASettingsFile: string);

LoadDefaults;

if not TFile.Exists(FSettingsFile) then
if ACreateFile and (not TFile.Exists(FSettingsFile)) then
begin
TLogger.Info('Settings file not found. Creating default settings: ' + FSettingsFile);
CreateDefaultSettingsFile;
Expand Down Expand Up @@ -104,8 +104,10 @@ function TMCPSettings.GetProtocol: string;
end;

procedure TMCPSettings.CreateDefaultSettingsFile;
var
IniFile: TIniFile;
begin
var IniFile := TIniFile.Create(FSettingsFile);
IniFile := TIniFile.Create(FSettingsFile);
try
IniFile.WriteString('Server', '; Server configuration', '');
IniFile.WriteInteger('Server', 'Port', FPort);
Expand All @@ -130,11 +132,13 @@ procedure TMCPSettings.CreateDefaultSettingsFile;
end;

procedure TMCPSettings.LoadFromFile;
var
IniFile: TIniFile;
begin
if not TFile.Exists(FSettingsFile) then
Exit;

var IniFile := TIniFile.Create(FSettingsFile);
IniFile := TIniFile.Create(FSettingsFile);
try
FPort := IniFile.ReadInteger('Server', 'Port', FPort);
FHost := IniFile.ReadString('Server', 'Host', FHost);
Expand Down Expand Up @@ -167,8 +171,10 @@ procedure TMCPSettings.LoadFromFile;
end;

procedure TMCPSettings.SaveToFile;
var
IniFile: TIniFile;
begin
var IniFile := TIniFile.Create(FSettingsFile);
IniFile := TIniFile.Create(FSettingsFile);
try
IniFile.WriteInteger('Server', 'Port', FPort);
IniFile.WriteString('Server', 'Host', FHost);
Expand Down
39 changes: 30 additions & 9 deletions src/Managers/MCPServer.CoreManager.pas
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,26 @@ function TMCPCoreManager.ExecuteMethod(const Method: string; const Params: TJSON
end;

function TMCPCoreManager.Initialize(const Params: TJSONObject): TValue;
var
Capabilities: TJSONObject;
ClientInfo: TJSONObject;
ClientName: TJSONValue;
ClientVersion: TJSONValue;
ResourcesCap: TJSONObject;
ResultJSON: TJSONObject;
ServerInfo: TJSONObject;
ToolsCap: TJSONObject;
begin
TLogger.Info('MCP Initialize called');

if Assigned(Params) then
begin
var ClientInfo := Params.GetValue('clientInfo') as TJSONObject;
ClientInfo := Params.GetValue('clientInfo') as TJSONObject;

if Assigned(ClientInfo) then
begin
var ClientName := ClientInfo.GetValue('name');
var ClientVersion := ClientInfo.GetValue('version');
ClientName := ClientInfo.GetValue('name');
ClientVersion := ClientInfo.GetValue('version');

if Assigned(ClientName) and Assigned(ClientVersion) then
TLogger.Info(Format('Client: %s v%s', [ClientName.Value, ClientVersion.Value]));
Expand All @@ -87,26 +96,36 @@ function TMCPCoreManager.Initialize(const Params: TJSONObject): TValue;

FSessionID := TGuid.NewGuid.ToString;

var ResultJSON := TJSONObject.Create;
ResultJSON := TJSONObject.Create;
try
ResultJSON.AddPair('protocolVersion', MCP_PROTOCOL_VERSION);

var Capabilities := TJSONObject.Create;
Capabilities := TJSONObject.Create;
ResultJSON.AddPair('capabilities', Capabilities);

var ToolsCap := TJSONObject.Create;
ToolsCap := TJSONObject.Create;
Capabilities.AddPair('tools', ToolsCap);
{$IF COMPILERVERSION <= 29}
ToolsCap.AddPair('supportsProgress', TJSONFalse.Create);
ToolsCap.AddPair('supportsCancellation', TJSONFalse.Create);
{$ELSE}
ToolsCap.AddPair('supportsProgress', TJSONBool.Create(False));
ToolsCap.AddPair('supportsCancellation', TJSONBool.Create(False));
{$ENDIF}

var ResourcesCap := TJSONObject.Create;
ResourcesCap := TJSONObject.Create;
Capabilities.AddPair('resources', ResourcesCap);
{$IF COMPILERVERSION <= 29}
ResourcesCap.AddPair('subscribe', TJSONFalse.Create);
ResourcesCap.AddPair('listChanged', TJSONFalse.Create);
{$ELSE}
ResourcesCap.AddPair('subscribe', TJSONBool.Create(False));
ResourcesCap.AddPair('listChanged', TJSONBool.Create(False));
{$ENDIF}

ResultJSON.AddPair('sessionId', FSessionID);

var ServerInfo := TJSONObject.Create;
ServerInfo := TJSONObject.Create;
ResultJSON.AddPair('serverInfo', ServerInfo);
ServerInfo.AddPair('name', FSettings.ServerName);
ServerInfo.AddPair('version', FSettings.ServerVersion);
Expand All @@ -121,10 +140,12 @@ function TMCPCoreManager.Initialize(const Params: TJSONObject): TValue;
end;

function TMCPCoreManager.Ping: TValue;
var
ResultJSON: TJSONObject;
begin
TLogger.Info('MCP Ping called');

var ResultJSON := TJSONObject.Create;
ResultJSON := TJSONObject.Create;
try
Result := TValue.From<TJSONObject>(ResultJSON);
except
Expand Down
54 changes: 35 additions & 19 deletions src/Managers/MCPServer.ResourcesManager.pas
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,32 @@ procedure TMCPResourcesManager.RegisterResource(const Resource: IMCPResource);
end;

procedure TMCPResourcesManager.RegisterBuiltInResources;
var
ResourceURI: string;
begin
for var ResourceURI in TMCPRegistry.GetResourceURIs do
for ResourceURI in TMCPRegistry.GetResourceURIs do
begin
RegisterResource(TMCPRegistry.CreateResource(ResourceURI));
end;
end;

function TMCPResourcesManager.ListResources: TValue;
var
Resource: IMCPResource;
ResourcesArray: TJSONArray;
ResourceObj: TJSONObject;
ResultJSON: TJSONObject;
begin
TLogger.Info('MCP ListResources called');
var ResultJSON := TJSONObject.Create;

ResultJSON := TJSONObject.Create;
try
var ResourcesArray := TJSONArray.Create;
ResourcesArray := TJSONArray.Create;
ResultJSON.AddPair('resources', ResourcesArray);

for var Resource in FResources.Values do
for Resource in FResources.Values do
begin
var ResourceObj := TJSONObject.Create;
ResourceObj := TJSONObject.Create;
ResourceObj.AddPair('uri', Resource.URI);
ResourceObj.AddPair('name', Resource.Name);
ResourceObj.AddPair('description', Resource.Description);
Expand All @@ -115,32 +122,38 @@ function TMCPResourcesManager.ListResources: TValue;
end;

function TMCPResourcesManager.ReadResource(const Params: System.JSON.TJSONObject): TValue;
var
ContentItem: TJSONObject;
ContentsArray: TJSONArray;
Resource: IMCPResource;
ResourceText: string;
ResultJSON: TJSONObject;
URI: string;
URIValue: TJSONValue;
begin
var URIValue := Params.GetValue('uri');
var URI: string;
URIValue := Params.GetValue('uri');
if Assigned(URIValue) then
URI := URIValue.Value
else
URI := '';

TLogger.Info('MCP ReadResource called for URI: ' + URI);
var ResultJSON := TJSONObject.Create;

ResultJSON := TJSONObject.Create;
try
var ContentsArray := TJSONArray.Create;
ContentsArray := TJSONArray.Create;
ResultJSON.AddPair('contents', ContentsArray);
var ContentItem := TJSONObject.Create;

ContentItem := TJSONObject.Create;
ContentsArray.AddElement(ContentItem);

var Resource: IMCPResource;

if FResources.TryGetValue(URI, Resource) then
begin
ContentItem.AddPair('uri', Resource.URI);
ContentItem.AddPair('mimeType', Resource.MimeType);

try
var ResourceText := Resource.Read;
ResourceText := Resource.Read;
ContentItem.AddPair('text', ResourceText);
except
on E: Exception do
Expand All @@ -164,12 +177,15 @@ function TMCPResourcesManager.ReadResource(const Params: System.JSON.TJSONObject
end;

function TMCPResourcesManager.ListResourceTemplates: TValue;
var
ResourceTemplatesArray: TJSONArray;
ResultJSON: TJSONObject;
begin
TLogger.Info('MCP ListResourceTemplates called');

var ResultJSON := TJSONObject.Create;
ResultJSON := TJSONObject.Create;
try
var ResourceTemplatesArray := TJSONArray.Create;
ResourceTemplatesArray := TJSONArray.Create;
ResultJSON.AddPair('resourceTemplates', ResourceTemplatesArray);

// Return empty array since this server doesn't support resource templates
Expand Down
Loading