Files
integration/Plugins/BlueprintMCP/Source/BlueprintMCP/Handlers/Blueprint_RemoveFunctionParameter.h
2026-03-14 00:02:00 -04:00

116 lines
3.4 KiB
C++

#pragma once
#include "CoreMinimal.h"
#include "MCPServer.h"
#include "MCPHandler.h"
#include "MCPFetcher.h"
#include "MCPUtils.h"
#include "K2Node_FunctionEntry.h"
#include "K2Node_CustomEvent.h"
#include "K2Node_EditablePinBase.h"
#include "Kismet2/BlueprintEditorUtils.h"
#include "Blueprint_RemoveFunctionParameter.generated.h"
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
UCLASS()
class UMCP_Blueprint_RemoveFunctionParameter : public UObject, public IMCPHandler
{
GENERATED_BODY()
public:
UPROPERTY(meta=(Description="Blueprint package path"))
FString Blueprint;
UPROPERTY(meta=(Description="Name of the function or custom event"))
FString FunctionName;
UPROPERTY(meta=(Description="Name of the parameter to remove"))
FString ParamName;
virtual FString GetDescription() const override
{
return TEXT("Remove a parameter from a function or custom event in a Blueprint.");
}
virtual void Handle() override
{
MCPFetcher F;
UBlueprint* BP = F.Asset(Blueprint).Cast<UBlueprint>();
if (!BP) return;
// Find the entry node (function entry or custom event)
UK2Node_EditablePinBase* EntryNode = nullptr;
for (UK2Node_FunctionEntry* FuncEntry : MCPUtils::AllNodes<UK2Node_FunctionEntry>(BP))
{
if (MCPUtils::Identifies(FunctionName, FuncEntry->GetGraph()))
{
EntryNode = FuncEntry;
break;
}
}
if (!EntryNode)
{
for (UK2Node_CustomEvent* CustomEvent : MCPUtils::AllNodes<UK2Node_CustomEvent>(BP))
{
if (CustomEvent->CustomFunctionName.ToString().Equals(FunctionName, ESearchCase::IgnoreCase))
{
EntryNode = CustomEvent;
break;
}
}
}
if (!EntryNode)
{
UMCPServer::Printf(TEXT("Error: Function or event '%s' not found.\nAvailable:\n"), *FunctionName);
for (UK2Node_FunctionEntry* FE : MCPUtils::AllNodes<UK2Node_FunctionEntry>(BP))
UMCPServer::Printf(TEXT(" function: %s\n"), *MCPUtils::FormatName(FE->GetGraph()));
for (UK2Node_CustomEvent* CE : MCPUtils::AllNodes<UK2Node_CustomEvent>(BP))
UMCPServer::Printf(TEXT(" event: %s\n"), *MCPUtils::FormatName(CE));
return;
}
// Find the parameter to remove
int32 RemovedIndex = INDEX_NONE;
for (int32 i = 0; i < EntryNode->UserDefinedPins.Num(); ++i)
{
if (EntryNode->UserDefinedPins[i].IsValid() &&
EntryNode->UserDefinedPins[i]->PinName.ToString().Equals(ParamName, ESearchCase::IgnoreCase))
{
RemovedIndex = i;
break;
}
}
if (RemovedIndex == INDEX_NONE)
{
UMCPServer::Printf(TEXT("Error: Parameter '%s' not found on %s.\nAvailable:\n"),
*ParamName, *MCPUtils::FormatName(EntryNode));
for (const TSharedPtr<FUserPinInfo>& PinInfo : EntryNode->UserDefinedPins)
if (PinInfo.IsValid())
UMCPServer::Printf(TEXT(" %s\n"), *PinInfo->PinName.ToString());
return;
}
// Remove the pin
EntryNode->UserDefinedPins.RemoveAt(RemovedIndex);
// Reconstruct the node to update output pins
if (UEdGraph* OwningGraph = EntryNode->GetGraph())
if (const UEdGraphSchema* Schema = OwningGraph->GetSchema())
Schema->ReconstructNode(*EntryNode);
bool bSaved = MCPUtils::SaveBlueprintPackage(BP);
UMCPServer::Printf(TEXT("Removed parameter '%s' from %s.\n"), *ParamName, *MCPUtils::FormatName(EntryNode));
if (!bSaved)
UMCPServer::Print(TEXT("Warning: save failed.\n"));
}
};