Skip to content

Commit 18c304a

Browse files
committed
MCP Client To Server 구현 by RemoteActor
1 parent c18d9f3 commit 18c304a

3 files changed

Lines changed: 64 additions & 15 deletions

File tree

McpServer/Program.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
.WithStdioServerTransport()
1717
.WithToolsFromAssembly();
1818

19-
builder.Services.AddSingleton<ActorService>();
19+
//var serverMode = args.Contains("--serverMode"); // 실행 인자에서 serverMode 설정
20+
21+
var clientMode = args.Contains("--clientMode"); // 실행 인자에서 clientMode 설정
22+
23+
builder.Services.AddSingleton<ActorService>(provider => new ActorService(!clientMode));
24+
2025
builder.Services.AddHostedService<ActorServiceInitializer>();
2126

2227
await builder.Build().RunAsync();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"profiles": {
3+
"McpServer": {
4+
"commandName": "Project",
5+
"commandLineArgs": "--serverMode"
6+
}
7+
}
8+
}

McpServer/Service/ActorService.cs

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using Akka.Actor;
2+
using Akka.Configuration;
3+
24
using McpServer.Actor;
35
using McpServer.Actor.Model;
46

@@ -14,23 +16,57 @@ public class ActorService
1416

1517
public IActorRef HistoryActor { get; set; }
1618

17-
public ActorService()
19+
public ActorService(bool serverMode)
1820
{
19-
actorSystem = ActorSystem.Create("MyActorSystem");
20-
21-
SearchActor = actorSystem.ActorOf<SearchActor>("search-actor");
22-
RecordActor = actorSystem.ActorOf<RecordActor>("record-actor");
23-
HistoryActor = actorSystem.ActorOf<HistoryActor>("history-actor");
24-
25-
RecordActor.Tell(new SetHistoryActorCommand()
21+
Console.WriteLine($"ActorService initialized in {(serverMode ? "Server" : "Client")} mode.");
22+
23+
// HOCON 설정
24+
var config = ConfigurationFactory.ParseString($@"
25+
akka {{
26+
actor {{
27+
provider = ""Akka.Remote.RemoteActorRefProvider, Akka.Remote""
28+
}}
29+
remote {{
30+
dot-netty.tcp {{
31+
hostname = ""127.0.0.1""
32+
port = {(serverMode ? 5500 : 0)} // 서버 모드일 때만 포트 5500 사용
33+
}}
34+
}}
35+
}}
36+
");
37+
38+
actorSystem = ActorSystem.Create("MyActorSystem", config);
39+
40+
if (serverMode)
2641
{
27-
HistoryActor = HistoryActor
28-
});
29-
30-
SearchActor.Tell(new SetHistoryActorCommand()
42+
// 서버 모드일 때만 특정 액터를 초기화
43+
SearchActor = actorSystem.ActorOf<SearchActor>("search-actor");
44+
RecordActor = actorSystem.ActorOf<RecordActor>("record-actor");
45+
HistoryActor = actorSystem.ActorOf<HistoryActor>("history-actor");
46+
47+
RecordActor.Tell(new SetHistoryActorCommand()
48+
{
49+
HistoryActor = HistoryActor
50+
});
51+
52+
SearchActor.Tell(new SetHistoryActorCommand()
53+
{
54+
HistoryActor = HistoryActor
55+
});
56+
}
57+
else
3158
{
32-
HistoryActor = HistoryActor
33-
});
59+
// 클라이언트 모드일 때 원격 액터 참조
60+
var remoteAddress = "akka.tcp://MyActorSystem@127.0.0.1:5500";
61+
SearchActor = actorSystem.ActorSelection($"{remoteAddress}/user/search-actor")
62+
.ResolveOne(TimeSpan.FromSeconds(3)).Result;
63+
64+
RecordActor = actorSystem.ActorSelection($"{remoteAddress}/user/record-actor")
65+
.ResolveOne(TimeSpan.FromSeconds(3)).Result;
66+
67+
HistoryActor = actorSystem.ActorSelection($"{remoteAddress}/user/history-actor")
68+
.ResolveOne(TimeSpan.FromSeconds(3)).Result;
69+
}
3470
}
3571

3672
}

0 commit comments

Comments
 (0)