10
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Posted at

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

GitHub

PyPI

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

種明かし

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

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

image.png

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

手順

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

  1. プロジェクト作成

    uv init
    
  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)」が表示されます。これを抑制するために、環境変数を追加します。

    import os
    
    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の中で使ってるライブラリーがうまく動いてなさそうです。

10
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?