MCP PowerShell助手
一种模型上下文协议服务器,使AI助手能够执行PowerShell命令、检索系统信息、管理模块以及在Windows系统上运行脚本。
可用工具 (6 个)
该服务在 MCP 协议中暴露的工具,AI 可按需调用
execute_ps 1 个参数 需填 1 项
必填参数:command
get_system_info
该工具无需必填参数,直接调用即可
list_modules
该工具无需必填参数,直接调用即可
get_command_help 1 个参数 需填 1 项
必填参数:command
find_commands 1 个参数 需填 1 项
必填参数:search
run_script 2 个参数 需填 1 项
必填参数:scriptPath
服务介绍
PowerShell MCP 服务器
用于与 PowerShell 交互的 Model Context Protocol 服务器。此服务器提供了执行 PowerShell 命令、检索系统信息、管理模块等功能。
要求
- Node.js 18+
- PowerShell 5.1 或 PowerShell Core 7+
安装
-
安装依赖项:
bash
npm install -
构建项目:
bash
npm run build
配置
对于 Claude Desktop
编辑配置文件:$HOME/Library/Application Support/Claude/claude_desktop_config.json
在 mcpServers 中添加以下内容:
json
{
"mcpServers": {
"mcp-powershell": {
"command": "node",
"args": [
"/absolute/path/to/mcp-powershell/dist/index.js"
]
}
}
}
对于 VS Code
编辑配置文件:$HOME/Library/Application Support/Code/User/settings.json
在设置中添加以下内容:
json
"mcp": {
"servers": {
"mcp-powershell": {
"command": "node",
"args": [
"/absolute/path/to/mcp-powershell/dist/index.js"
]
}
}
}
对于 Cursor IDE
编辑配置文件:$HOME/.cursor/mcp.json
在 mcpServers 中添加以下内容:
json
{
"mcpServers": {
"mcp-powershell": {
"command": "node",
"args": [
"/absolute/path/to/mcp-powershell/dist/index.js"
]
}
}
}
可用工具
此 PowerShell MCP 服务器提供以下工具:
execute_ps
执行一个 PowerShell 命令并获取结果。
参数:
- command (string): 要执行的 PowerShell 命令
使用示例:
execute_ps(command: "Get-Process | Select-Object -First 5")
get_system_info
检索详细的系统信息,包括操作系统详情、处理器、内存和 PowerShell 版本。
参数:无
使用示例:
get_system_info()
list_modules
列出所有已安装的 PowerShell 模块及其详细信息,如名称、版本和类型。
参数:无
使用示例:
list_modules()
get_command_help
获取特定 PowerShell 命令的详细帮助,包括语法、参数和示例。
参数:
- command (string): 要获取帮助的 PowerShell 命令
使用示例:
get_command_help(command: "Get-Process")
find_commands
按名称或模式搜索 PowerShell 命令。
参数:
- search (string): PowerShell 命令的搜索词
使用示例:
find_commands(search: "Process")
run_script
运行一个 PowerShell 脚本文件,并可选地传递参数。
参数:
- scriptPath (string): PowerShell 脚本文件的路径
- parameters (string, optional): 传递给脚本的可选参数
使用示例:
run_script(scriptPath: "/path/to/script.ps1", parameters: "-Name Test -Value 123")
开发
以开发模式运行:
bash
npm run dev
扩展服务器
要添加自己的 PowerShell 工具:
- 编辑
src/index.ts - 在
registerTools()方法中添加新工具 - 遵循现有的错误处理模式
- 使用
npm run build进行构建
添加工具示例
typescript
// 在 registerTools() 方法中:
this.server.tool(
"my_ps_tool",
{
param1: z.string().describe("参数 1 的描述"),
param2: z.number().optional().describe("可选的数值参数"),
},
async ({ param1, param2 }) => {
try {
// 你的 PowerShell 命令
const command = Your-PowerShell-Command -Param1 "${param1}" ${param2 ? -Param2 ${param2} : ''};
const { stdout, stderr } = await execAsync(`powershell -Command "${command.replace(/"/g, '\\"')}"`);
if (stderr) {
return {
isError: true,
content: [
{
type: "text" as const,
text: `my_ps_tool 错误: ${stderr}`,
},
],
};
}
return {
content: [
{
type: "text" as const,
text: stdout,
},
],
};
} catch (error) {
return {
isError: true,
content: [
{
type: "text" as const,
text: `my_ps_tool 错误: ${(error as Error).message}`,
},
],
};
}
}
);## 安全注意事项
- 该服务器直接在您的系统上执行 PowerShell 命令
- 命令以运行 MCP 服务器进程的相同权限执行
- 在暴露破坏性操作时需谨慎
- 考虑为敏感命令实现额外的验证
故障排除
常见问题
-
PowerShell 执行策略限制
- 您可能需要调整 PowerShell 执行策略以允许脚本执行
- 使用
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser允许本地脚本执行
-
找不到路径错误
- 确保文件路径是绝对路径或相对于工作目录的正确相对路径
- 使用适合您操作系统的路径分隔符
-
找不到命令错误
- 某些命令可能需要安装特定模块
- 使用
Install-Module ModuleName安装所需的模块
许可证
MIT