diff --git a/Base/GiteaHelp.cs b/Base/GiteaHelp.cs new file mode 100644 index 0000000..c55f7b1 --- /dev/null +++ b/Base/GiteaHelp.cs @@ -0,0 +1,84 @@ +using Flurl.Http; +using Newtonsoft.Json.Linq; + +namespace GiteaHelpMCP.Base +{ + public class GiteaHelp + { + [McpTool("GetReposFullName", "得到当前用户所有的仓库,返回仓库的full_name列表")] + public static string GetReposFullName() + { + var url = MsBase.url + "/api/v1/user/repos?page=1&limit=100"; + var resultStr = url.WithHeader("accept", "application/json") + .WithHeader("Authorization", $"token {MsBase.token}") + .GetStringAsync().GetAwaiter().GetResult(); + var result = JArray.Parse(resultStr); + var resultPath = "$[*].full_name"; + IEnumerable resultTokens = result.SelectTokens(resultPath); + var newresultObjects = resultTokens.Select(result => + { + return result.ToString(); + }).ToList(); + return JArray.FromObject(newresultObjects).ToString(Newtonsoft.Json.Formatting.None); + } + + [McpTool("GetCommits", "得到指定仓库的commit,返回详细的信息json")] + public static string GetCommits( + [McpParameter(true, "仓库")] string repoFullName, + [McpParameter(true, "page 默认是1")] int page = 1, + [McpParameter(true, "limit 默认是20")] int limit = 20) + { + var url = MsBase.url + $"/api/v1/repos/{repoFullName}/commits?page={page}&limit={limit}"; + var resultStr = url.WithHeader("accept", "application/json") + .WithHeader("Authorization", $"token {MsBase.token}") + .GetStringAsync().GetAwaiter().GetResult(); + //var result = JArray.Parse(resultStr); + return resultStr; + } + + [McpTool("GetCommitsSimple", "得到仓库的commit,返回简单信息json,[仓库,创建时间,提交信息]")] + public static string GetCommitsSimple( + [McpParameter(true, "仓库")] string repoFullName, + [McpParameter(true, "page 默认是1")] int page = 1, + [McpParameter(true, "limit 默认是20")] int limit = 20) + { + var resultStr = GetCommits(repoFullName, page, limit); + var result = JArray.Parse(resultStr); + var json = new JArray(); + foreach (JObject item in result) + { + // 获取 created 字段 + string created = item["created"].ToString(); + // 获取 commit 对象的 message 字段 + string message = item["commit"]["message"].ToString(); + json.Add(new JObject { + {"repoFullName",repoFullName }, + { "created", created }, + { "message", message }}); + } + return json.ToString(Newtonsoft.Json.Formatting.None); + } + + [McpTool("GetAllReposCommitsSimple", "得到所有仓库的commit,返回简单信息json,[[仓库,创建时间,提交信息]];可以通过limit调整返回的数量,默认是20,最大是100;注意这里的分页是单个仓库的提交记录分页,不是所有仓库的提交记录分页")] + public static string GetAllReposCommitsSimple( + [McpParameter(true, "page 默认是1")] int page = 1, + [McpParameter(true, "limit 默认是20")] int limit = 20) + { + if (limit > 100) { limit = 100; } + var AllReposstr = GetReposFullName(); + var AllRepos = JArray.Parse(AllReposstr); + var json = new JArray(); + foreach (var item in AllRepos) + { + string Commits = GetCommitsSimple(item.ToString(), page, limit); + //判断Commits是否为空 + var Commitsjs = JArray.Parse(Commits); + if (Commitsjs.Any()) + { + json.Add(Commitsjs); + } + } + return json.ToString(Newtonsoft.Json.Formatting.None); + } + } +} diff --git a/Base/MsBase.cs b/Base/MsBase.cs new file mode 100644 index 0000000..df8f635 --- /dev/null +++ b/Base/MsBase.cs @@ -0,0 +1,8 @@ +namespace GiteaHelpMCP.Base +{ + public class MsBase + { + public static string token { get; set; } + public static string url { get; set; } + } +} diff --git a/GiteaHelpMCP.csproj b/GiteaHelpMCP.csproj new file mode 100644 index 0000000..aace971 --- /dev/null +++ b/GiteaHelpMCP.csproj @@ -0,0 +1,21 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + + + + + + + + diff --git a/GiteaHelpMCP.sln b/GiteaHelpMCP.sln new file mode 100644 index 0000000..1f99ce6 --- /dev/null +++ b/GiteaHelpMCP.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.12.35728.132 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GiteaHelpMCP", "GiteaHelpMCP.csproj", "{A06846D9-EB56-4674-B89E-4A7F14B548F9}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A06846D9-EB56-4674-B89E-4A7F14B548F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A06846D9-EB56-4674-B89E-4A7F14B548F9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A06846D9-EB56-4674-B89E-4A7F14B548F9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A06846D9-EB56-4674-B89E-4A7F14B548F9}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..545911f --- /dev/null +++ b/Program.cs @@ -0,0 +1,39 @@ +// See https://aka.ms/new-console-template for more information +global using MCPSharp; +using GiteaHelpMCP.Base; + +public class Program +{ + public static async Task Main(string[] args) + { + GetArgs(args); + Console.WriteLine("Hello, GiteaHelpMCPServer!"); + GiteaHelp.GetAllReposCommitsSimple(); + await MCPServer.StartAsync("GiteaHelpMCPServer", "1.0.0"); + Console.WriteLine("Server is running. Press any key to stop."); + Console.ReadKey(); + } + + public static void GetArgs(string[] args) + { + foreach (var arg in args) + { + if (arg.Contains("=")) + { + var argName = arg.Split('=')[0]; + var argValue = arg.Split('=')[1]; + //和 MsBase 的属性一一对应 并赋值 + switch (argName) + { + case "url": + MsBase.url = argValue; + break; + + case "token": + MsBase.token = argValue; + break; + } + } + } + } +} diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json new file mode 100644 index 0000000..ca99a0b --- /dev/null +++ b/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "GiteaHelpMCP": { + "commandName": "Project", + "commandLineArgs": "token=838866be705cc14e13c9f03b637e29a1624983da url=http://192.168.123.44:9000" + } + } +} \ No newline at end of file