Commit 8e87cd73 authored by shrabbit's avatar shrabbit
Browse files

feat(ProcessState): 新增进程信息查询工具并注册到 MCP 服务,升级相关依赖包

parent 02fb9e75
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -33,8 +33,9 @@
    </ItemGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1"/>
        <PackageReference Include="ModelContextProtocol" Version="1.2.0"/>
        <PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.10" />
        <PackageReference Include="ModelContextProtocol" Version="2.1.0" />
        <PackageReference Include="shRabbit.Base" Version="1.12.1" />
    </ItemGroup>

</Project>
+6 −2
Original line number Diff line number Diff line
@@ -10,8 +10,12 @@ builder.Logging.AddConsole(o => o.LogToStandardErrorThreshold = LogLevel.Trace);

// Add the MCP services: the transport to use (stdio) and the tools to register.
builder.Services
    .AddMcpServer()
    .AddMcpServer(c =>
    {
        
    })
    .WithStdioServerTransport()
    .WithTools<RandomNumberTools>();
    .WithTools<RandomNumberTools>()
    .WithTools<ProcessState>();

await builder.Build().RunAsync();
 No newline at end of file
+74 −0
Original line number Diff line number Diff line
using System.ComponentModel;
using System.Diagnostics;
using System.Text.Json.Nodes;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
using shRabbit.Base;

namespace McpBase.McpServer.Tools;

public class ProcessState
{
    [McpServerTool(UseStructuredContent = true), Description("From program name get process basic information (like PID, name, main path, title, etc.).")]
    public IEnumerable<ProcessStateBase> GetProcess([Description("Process Name")]string processName)
    {
        foreach (var process in Process.GetProcessesByName(processName))
        {
            var title = process.MainWindowTitle;
            if (string.IsNullOrEmpty(title))
                title = null;
            
            yield return new ProcessStateBase
            {
                ProcessId = process.Id,
                Name = process.ProcessName,
                Path = process.MainModule?.FileName ?? null,
                Title = title
            };
        }
    }

    [McpServerTool(UseStructuredContent = true),
     Description("Get detailed information of a process by its PID. Returns basic info (name, main module path, main window title) and memory statistics (private memory, virtual memory, peak virtual memory, working set, peak working set, paged memory, paged system memory, nonpaged system memory). All memory sizes are formatted as human-readable strings via ByteSize.ToString().")]
    public ProcessStateDetail GetProcessDetail([Description("Target process ID (PID)")] int processId)
    {
        var process = Process.GetProcessById(processId);

        var title = process.MainWindowTitle;
        return new ProcessStateDetail
        {
            ProcessId = process.Id,
            Name = process.ProcessName,
            Path = process.MainModule?.FileName ?? null,
            Title = title,
            PrivateMemorySize = ByteSize.FromBytes(process.PrivateMemorySize64).ToString(),
            VirtualMemorySize = ByteSize.FromBytes(process.VirtualMemorySize64).ToString(),
            VirtualPeakMemorySize = ByteSize.FromBytes(process.PeakVirtualMemorySize64).ToString(),
            WorkingSet = ByteSize.FromBytes(process.WorkingSet64).ToString(),
            NonpagedSystemMemorySize = ByteSize.FromBytes(process.NonpagedSystemMemorySize64).ToString(),
            PeakWorkingSet = ByteSize.FromBytes(process.PeakWorkingSet64).ToString(),
            PagedMemorySize = ByteSize.FromBytes(process.PagedMemorySize64).ToString(),
            PagedSystemMemorySize = ByteSize.FromBytes(process.PagedSystemMemorySize64).ToString(),
        };
    }
}

public class ProcessStateBase
{
    public int ProcessId { get; set; }
    public string Name { get; set; } = string.Empty;
    public string? Path { get; set; }
    public string? Title { get; set; }
}

public class ProcessStateDetail : ProcessStateBase
{
    public string PrivateMemorySize { get; set; } = string.Empty;
    public string VirtualMemorySize { get; set; } = string.Empty;
    public string VirtualPeakMemorySize { get; set; } = string.Empty;
    public string WorkingSet { get; set; } = string.Empty;
    public string NonpagedSystemMemorySize { get; set; } = string.Empty;
    public string PeakWorkingSet { get; set; } = string.Empty;
    public string PagedMemorySize { get; set; } = string.Empty;
    public string PagedSystemMemorySize { get; set; } = string.Empty;
}
 No newline at end of file