添加项目文件。
This commit is contained in:
parent
20dbad2e55
commit
9a0c5ffa2c
84
Base/GiteaHelp.cs
Normal file
84
Base/GiteaHelp.cs
Normal file
@ -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<JToken> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
8
Base/MsBase.cs
Normal file
8
Base/MsBase.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace GiteaHelpMCP.Base
|
||||
{
|
||||
public class MsBase
|
||||
{
|
||||
public static string token { get; set; }
|
||||
public static string url { get; set; }
|
||||
}
|
||||
}
|
||||
21
GiteaHelpMCP.csproj
Normal file
21
GiteaHelpMCP.csproj
Normal file
@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Flurl" Version="4.0.0" />
|
||||
<PackageReference Include="Flurl.Http" Version="4.0.2" />
|
||||
<PackageReference Include="MCPSharp" Version="1.0.10" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Properties\PublishProfiles\" />
|
||||
<Folder Include="Tool\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
22
GiteaHelpMCP.sln
Normal file
22
GiteaHelpMCP.sln
Normal file
@ -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
|
||||
39
Program.cs
Normal file
39
Program.cs
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
8
Properties/launchSettings.json
Normal file
8
Properties/launchSettings.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"profiles": {
|
||||
"GiteaHelpMCP": {
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "token=838866be705cc14e13c9f03b637e29a1624983da url=http://192.168.123.44:9000"
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user