Commit 4d01e043 authored by shrabbit's avatar shrabbit
Browse files

feat(ProcessState): 新增 GetProcessStdout 方法以读取进程标准输出

parent 7faad780
Loading
Loading
Loading
Loading
+37 −4
Original line number Diff line number Diff line
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using JetBrains.Annotations;
using ModelContextProtocol.Server;
using shRabbit.Base;
@@ -8,7 +9,8 @@ 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.).")]
    [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))
@@ -28,7 +30,8 @@ public class ProcessState
    }

    [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().")]
     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);
@@ -50,6 +53,36 @@ public class ProcessState
            PagedSystemMemorySize = ByteSize.FromBytes(process.PagedSystemMemorySize64).ToString(),
        };
    }

    [McpServerTool, Description("read process stdout.")]
    public string GetProcessStdout(
        [Description("Target process ID (PID)")] int processId,
        [Description("Max lines (negative is infinite)")] int maxLines = -1)
    {
        try
        {
            var process = Process.GetProcessById(processId);
            var stdout = process.StandardOutput;

            if (maxLines < 0)
            {
                return stdout.ReadToEnd();
            }

            var sb = new StringBuilder();
            for (int i = 0; i < maxLines; i++)
            {
                sb.AppendLine(stdout.ReadLine());
            }

            return sb.ToString();
        }
        catch (InvalidOperationException)
        {
            return "Cannot read stdout: process may have terminated or does not support output redirection.";
        }
    }

}

public class ProcessStateBase