金曜日, 5月 23, 2025
ホームニューステックニュース禁断の(?)MCPツール`use_aws`を作ってしまいました #AWS - Qiita

禁断の(?)MCPツール`use_aws`を作ってしまいました #AWS – Qiita



禁断の(?)MCPツール`use_aws`を作ってしまいました #AWS - Qiita

何が禁断かわかりませんが、AWSのリソースにアクセスするMCPツールを作成しました🎉🎉🎉

GitHub

PyPI

無保証ですが使ってみてね。

種明かし

先週AWSがリリースした「Strands Agents」にはExample Built-in Toolsが用意されています。

Exampleってことなので、そのつもりでということですが、めっちゃたくさんツールが用意されています。

image.png

この中に、use_awsというAWSの任意のAPIを呼び出すツールが用意されています。
こいつをMCPサーバーにしてみましたということです。

手順

簡単にですが、構築手順をご紹介します。

  1. プロジェクト作成

  2. ライブラリーをインストール
    MCPのSDKも勝手に入ります

    uv add strands-agents-tools
    
  3. MCPサーバーの雛形を作成

    from mcp.server.fastmcp import FastMCP
    
    mcp = FastMCP("use-aws-mcp-server")
    
    def main():
        mcp.run()
    
    
    if __name__ == "__main__":
        main()
    
  4. ツールの実装を追加

    strands_toolsのuse_awsをインポートして呼ぶだけっす。

    import uuid
    
    from strands_tools import use_aws as strands_tools_use_aws
    
    @mcp.tool()
    def use_aws(
        service_name: str,
        operation_name: str,
        label: str,
        region: str = None,
        parameters: dict = {},
        profile_name: str = None,
    ) -> str:
        """
        Make an AWS CLI api call with the specified service, operation, and parameters. All arguments MUST conform to the AWS CLI specification. Should the output of the invocation indicate a malformed command, invoke help to obtain the the correct command.
    
        Args:
            service_name (str): The name of the AWS service. If you want to query s3, you should use s3api if possible.
            operation_name (str): The name of the operation to perform. You should also prefer snake case.
            label (str): Human readable description of the api that is being called.
            region (str): Region name for calling the operation on AWS.
            parameters (dict): The parameters for the operation. The parameter keys MUST conform to the AWS CLI specification. You should prefer to use JSON Syntax over shorthand syntax wherever possible. For parameters that are booleans, prioritize using flags with no value. Denote these flags with flag names as key and an empty string as their value. You should also prefer kebab case.
            profile_name (str): Optional: AWS profile name to use from ~/.aws/credentials. Defaults to default profile if not specified.
        """
    
        tool_use_id = str(uuid.uuid4())[:8]
        name = "use_aws"
    
        input_dict = {
            "service_name": service_name,
            "operation_name": operation_name,
            "region": region,
            "label": label,
        }
    
        if parameters is not None:
            input_dict["parameters"] = parameters
        if profile_name is not None:
            input_dict["profile_name"] = profile_name
    
        result = strands_tools_use_aws.use_aws(
            {
                "toolUseId": tool_use_id,
                "name": name,
                "input": input_dict,
            }
        )
    
        return result["content"][0]["text"]
    
    
  5. 環境変数を追加
    strands_toolsの動作仕様で、ツール実行のタイミングで標準入力に「ツール実行していいですか?(Y/N)」が表示されます。これを抑制するために、環境変数を追加します。

    os.environ["DEV"] = "true"
    

はい。こんだけです。

使い方

Amazon Q DeveloperのCLIは標準でuse_awsツールが含まれているので、GitHub Copilotでの設定です。

{
    "servers": {
        "use-aws-mcp-server": {
            "command": "uvx",
            "args": [
                "use-aws-mcp-server@latest"
            ],
            "env": {
                "AWS_REGION": "${input:aws_region}",
                "AWS_PROFILE": "${input:aws_profile}"
            }
        }
    },
    "inputs": [
        {
            "id": "aws_region",
            "type": "promptString",
            "description": "Enter the AWS region"
        },
        {
            "id": "aws_profile",
            "type": "promptString",
            "description": "Enter the AWS profile name"
        }
    ]
}

Windowsではなんだか動きません。strands_toolsの中で使ってるライブラリーがうまく動いてなさそうです。





Source link

Views: 0

RELATED ARTICLES

返事を書く

あなたのコメントを入力してください。
ここにあなたの名前を入力してください

- Advertisment -

インモビ転職