JavaScriptを有効にしてください

AIShell を MCP ホストとして使い隊

 ·   4 分で読めます  ·   Kento

AIShell の Preview 6 で MCP ホストとして利用できるようになりました。
今回はその設定方法を紹介します。

想定される読者

  • AIShell を利用してみたい方
  • AIShell を MCP ホストとして利用したい方
  • VSCode 以外で MCP を利用したい方

ざっくり手順

  1. AIShell のインストール
  2. Azure OpenAI のリソース作成
  3. AIShell の設定変更
  4. MCP ホストとして利用
  5. MCP サーバーの追加

1. AIShell のインストール

AI シェルをインストールする - PowerShell | Microsoft Learn に書かれている手順で AIShell をインストールします。

インストールに成功すると Start-AIShell コマンドで AIShell を起動できますが、以下のようなエラーが出る場合があります。

Start-AIShell: The 'Start-AIShell' command was found in the module 'AIShell', but the module could not be loaded due to the following error: [The PSReadLine v2.4.3-beta3 or higher is required for the AIShell module to work properly.] For more information, run 'Import-Module AIShell'.

エラーメッセージに従って Import-Module AIShell を実行すると、PSReadLine のバージョンが古いことが原因であることがわかります。
私の場合は適切なバージョンの PSReadLine をインストールしていましたが、古いバージョンが認識されていたため起動できていませんでした

Remove-Module PSReadLine

で古いバージョンをアンロードしてから

Import-Module PSReadLine -RequiredVersion 2.4.4 -Force

で最新バージョンを再度インポートした後、Start-AIShell を実行したところ、無事起動できました。

2. Azure OpenAI のリソース作成

Azure OpenAI のリソースを作成します。
AIShell で利用できるモデルは OpenAI エージェント - PowerShell | Microsoft Learn で記載されています
記事執筆時は下記のものが記載されていました。今回は gpt-4.1 を利用しました

  • o1
  • o3
  • o4-mini
  • gpt-4.1
  • gpt-4o
  • gpt-4
  • gpt-4-32k
  • gpt-4-turbo
  • gpt-3.5-turbo
  • gpt-35-turbo

3. AIShell の設定変更

AIShell の設定ファイルを編集します。
AIShell のチャット画面で /agent config openai-gpt と入力すると設定ファイルが開きます
OpenAI agent - PowerShell | Microsoft Learn を参考に設定ファイルを編集します。

サンプルはこちらです

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{
  // Declare GPT instances.
  "GPTs": [
    // To use the Azure OpenAI service:
    // - Set `Endpoint` to the endpoint of your Azure OpenAI service,
    //   or the endpoint to the Azure API Management service if you are using it as a gateway.
    // - Set `Deployment` to the deployment name of your Azure OpenAI service.
    // - Set `ModelName` to the name of the model used for your deployment, e.g. "gpt-4-0613".
    // - Set `Key` to the access key of your Azure OpenAI service,
    //   or the key of the Azure API Management service if you are using it as a gateway.
    {
      "Name": "ps-az-gpt4",
      "Description": "A GPT instance with expertise in PowerShell scripting and command line utilities. Use gpt-4 running in Azure.",
      "Endpoint": "<insert your Azure OpenAI endpoint>",
      "Deployment": "<insert your deployment name>",
      "ModelName": "<insert the model name>",   // required field to infer properties of the service, such as token limit.
      "Key": "<insert your key>",
      "SystemPrompt": "1. You are a helpful and friendly assistant with expertise in PowerShell scripting and command line.\n2. Assume user is using the operating system `Windows 11` unless otherwise specified.\n3. Use the `code block` syntax in markdown to encapsulate any part in responses that is code, YAML, JSON or XML, but not table.\n4. When encapsulating command line code, use '```powershell' if it's PowerShell command; use '```sh' if it's non-PowerShell CLI command.\n5. When generating CLI commands, never ever break a command into multiple lines. Instead, always list all parameters and arguments of the command on the same line.\n6. Please keep the response concise but to the point. Do not overexplain."
    },

    // To use the public OpenAI service:
    // - Ignore the `Endpoint` and `Deployment` keys.
    // - Set `ModelName` to the name of the model to be used.
    // - Set `Key` to be the OpenAI access token.
    // For example:
    {
        "Name": "ps-gpt4o",
        "Description": "A GPT instance with expertise in PowerShell scripting and command line utilities. Use gpt-4o running in OpenAI.",
        "ModelName": "gpt-4o",
        "Key": "<insert your key>",
        "SystemPrompt": "1. You are a helpful and friendly assistant with expertise in PowerShell scripting and command line.\n2. Assume user is using the operating system `Windows 11` unless otherwise specified.\n3. Use the `code block` syntax in markdown to encapsulate any part in responses that is code, YAML, JSON or XML, but not table.\n4. When encapsulating command line code, use '```powershell' if it's PowerShell command; use '```sh' if it's non-PowerShell CLI command.\n5. When generating CLI commands, never ever break a command into multiple lines. Instead, always list all parameters and arguments of the command on the same line.\n6. Please keep the response concise but to the point. Do not overexplain."
    }
  ],

  // Specify the default GPT instance to use for user query.
  "Active": "ps-az-gpt4"
}

4. MCP ホストとして利用

AIShell のチャット画面で /mcp と入力すると 利用可能な MCP Server とツールが表示されます
既定では AIShell の MCP サーバーのみが表示されます

image01
MCP ホストとして利用:

例えば get_working_directory ツールを利用すると、PowerShell のカレントディレクトリの情報を取得できます

image02
get_working_directory ツールの利用:

さらに run_command_in_terminal ツールを利用すると、PowerShell のコマンドを実行できます

image03
run_command_in_terminal ツールの利用:

5. MCP サーバーの追加

MCP のサポート - PowerShell | Microsoft Learn を参考に MCP サーバーを追加します。

MCP サーバーを追加するには、$HOME.aish\フォルダーにmcp.jsonファイルを作成します。

とのことなので、以下の内容で mcp.json ファイルを作成しました

1
2
3
4
5
6
7
8
{
    "servers": {
        "microsoft.docs.mcp": {
            "type": "http",
            "url": "https://learn.microsoft.com/api/mcp"
        }
    }
}

/mcp と入力すると、追加した MCP サーバーが表示されるようになりました

image04
MCP サーバーの追加:

実際に使ってみた動画がこちらです

ちゃんと Microsoft Learn の MCP Server を利用できています

まとめ

AIShell を MCP ホストとして利用する方法を紹介しました。
Microsoft 公式ツールで MCP が利用できるのは Visual Studio Code くらいしかなかったはずなので、選択肢が増えるのは嬉しいですね。

欠点として 明示的にツールを指定する方法がわからずプロンプトを工夫して使用しました。
もしツールを指定する方法が実装されていないのであれば ぜひ実装してほしいところです

参考

共有

Kento
著者
Kento
2020年に新卒で IT 企業に入社. インフラエンジニア(主にクラウド)として活動中