AWS S3 MCP Server
An MCP (Model Context Protocol) server that provides comprehensive AWS S3 management capabilities. Built with the fastmcp framework and boto3, this server exposes 14 tools for bucket operations, object management, access control, metadata tagging, and cost analysis.
Table of Contents
- Features
- Requirements
- Installation
- Configuration
- AWS Credentials
- Required IAM Permissions
- Usage
- Starting the Server
- Using with MCP Clients
- Tools Reference
- Bucket Management
- Object Operations
- Access Control
- Metadata & Tagging
- Cost Analysis
- Error Handling
- Troubleshooting
- Development
- License
Features
Bucket Management
- list_buckets: List all S3 buckets in your AWS account
- create_bucket: Create new S3 buckets with optional region specification
- delete_bucket: Delete empty S3 buckets
- get_bucket_region: Retrieve the AWS region of a bucket
Object Operations
- list_objects: List objects in a bucket with optional prefix filtering and pagination
- get_object_metadata: Retrieve metadata for S3 objects
- upload_object: Upload files to S3 with custom metadata and content types
- delete_object: Delete objects from S3
- copy_object: Copy objects within S3 (preserves metadata)
- move_object: Move objects within S3 (atomic copy-then-delete)
Access Control
- generate_presigned_download_url: Generate time-limited download URLs
Metadata & Tagging
- add_tags: Add or update tags on S3 objects
- search_by_tag: Search for objects by tag key and value
Cost Analysis
- calculate_bucket_storage_cost: Calculate estimated monthly storage costs
Requirements
- Python 3.8 or higher
- AWS account with appropriate S3 permissions
- AWS credentials configured (see Configuration section)
Installation
From Source```bash
Clone the repository
git clone <repository-url>
cd aws-s3-mcp-server
Install dependencies
pip install -r requirements.txt
Install the package in development mode
pip install -e .
### Using pip```bash
pip install aws-s3-mcp-server
Configuration
AWS Credentials
The server uses the standard AWS credential chain. Configure your credentials using one of these methods:
1. Environment Variables```bash
export AWS_ACCESS_KEY_ID=your_access_key
export AWS_SECRET_ACCESS_KEY=your_secret_key
export AWS_SESSION_TOKEN=your_session_token # Optional, for temporary credentials
export AWS_DEFAULT_REGION=us-east-1 # Optional
#### 2. AWS Credentials File
Create or edit `~/.aws/credentials`:```ini
[default]
aws_access_key_id = your_access_key
aws_secret_access_key = your_secret_key
Optionally, configure the default region in ~/.aws/config:```ini
[default]
region = us-east-1
#### 3. IAM Role (AWS Infrastructure)
When running on AWS infrastructure (EC2, ECS, Lambda), the server automatically uses the IAM role attached to the instance. No additional configuration is required.
### Required IAM Permissions
The AWS credentials must have the following S3 permissions:```json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:ListAllMyBuckets",
"s3:CreateBucket",
"s3:DeleteBucket",
"s3:GetBucketLocation",
"s3:ListBucket",
"s3:GetObject",
"s3:GetObjectMetadata",
"s3:PutObject",
"s3:DeleteObject",
"s3:CopyObject",
"s3:GetObjectTagging",
"s3:PutObjectTagging"
],
"Resource": "*"
}
]
}
For production environments, consider restricting the Resource field to specific buckets:```json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:ListAllMyBuckets"],
"Resource": ""
},
{
"Effect": "Allow",
"Action": [
"s3:CreateBucket",
"s3:DeleteBucket",
"s3:GetBucketLocation",
"s3:ListBucket"
],
"Resource": "arn:aws:s3:::your-bucket-prefix-"
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:GetObjectMetadata",
"s3:PutObject",
"s3:DeleteObject",
"s3:CopyObject",
"s3:GetObjectTagging",
"s3:PutObjectTagging"
],
"Resource": "arn:aws:s3:::your-bucket-prefix-/"
}
]
}
## Usage
### Starting the Server```bash
# Run the server
aws-s3-mcp-server
# Or run directly with Python
python -m aws_s3_mcp_server.server
The server will:
1. Load AWS credentials from the credential chain
2. Initialize the boto3 S3 client
3. Register all 14 tools
4. Start listening for MCP client connections
If credentials are not found, the server will log an error and exit with a non-zero status code.
Using with MCP Clients
Once the server is running, MCP clients can connect and invoke the available tools. Refer to your MCP client documentation for connection details.
Tools Reference
All tools return JSON-formatted responses with a consistent structure:
Success Response:```json
{
"success": true,
"data": {
// Tool-specific data
}
}
**Error Response:**```json
{
"success": false,
"error": {
"message": "Descriptive error message",
"code": "AWS_ERROR_CODE",
"type": "ErrorType"
}
}
Bucket Management
list_buckets
List all S3 buckets in the AWS account.
Parameters: None
Returns:```json
{
"success": true,
"data": {
"buckets": [
{
"name": "my-bucket",
"creation_date": "2024-01-01T00:00:00+00:00"
}
]
}
}
**Example:**```python
result = list_buckets()
create_bucket
Create a new S3 bucket.
Parameters:
- bucket_name (string, required): Name of the bucket to create (must be globally unique)
- region (string, optional): AWS region for the bucket (default: us-east-1)
Returns:```json
{
"success": true,
"data": {
"bucket_name": "my-new-bucket",
"region": "us-west-2",
"message": "Bucket 'my-new-bucket' created successfully in region us-west-2"
}
}
**Example:**```python
# Create bucket in default region (us-east-1)
result = create_bucket("my-new-bucket")
# Create bucket in specific region
result = create_bucket("my-new-bucket", region="us-west-2")
Notes:
- Bucket names must be globally unique across all AWS accounts
- Bucket names must be 3-63 characters long
- Bucket names can contain lowercase letters, numbers, and hyphens
- Bucket names must start and end with a letter or number
delete_bucket
Delete an S3 bucket.
Parameters:
- bucket_name (string, required): Name of the bucket to delete
Returns:```json
{
"success": true,
"data": {
"bucket_name": "my-old-bucket",
"message": "Bucket 'my-old-bucket' deleted successfully"
}
}
**Example:**```python
result = delete_bucket("my-old-bucket")
Notes:
- The bucket must be empty before it can be deleted
- If the bucket contains objects, the operation will fail with a BucketNotEmpty error
get_bucket_region
Get the AWS region where a bucket is located.
Parameters:
- bucket_name (string, required): Name of the bucket
Returns:```json
{
"success": true,
"data": {
"bucket_name": "my-bucket",
"region": "us-west-2"
}
}
**Example:**```python
result = get_bucket_region("my-bucket")
Object Operations
list_objects
List objects in an S3 bucket.
Parameters:
- bucket_name (string, required): Name of the bucket
- prefix (string, optional): Filter objects by key prefix (default: "")
- max_keys (integer, optional): Maximum number of objects to return (default: 1000)
Returns:```json
{
"success": true,
"data": {
"bucket_name": "my-bucket",
"prefix": "docs/",
"objects": [
{
"key": "docs/file1.txt",
"size": 1024,
"last_modified": "2024-01-01T00:00:00+00:00"
}
],
"count": 1
}
}
**Example:**```python
# List all objects
result = list_objects("my-bucket")
# List objects with prefix
result = list_objects("my-bucket", prefix="docs/")
# List with limit
result = list_objects("my-bucket", prefix="docs/", max_keys=10)
get_object_metadata
Get metadata for an S3 object.
Parameters:
- bucket_name (string, required): Name of the bucket
- object_key (string, required): Key of the object
Returns:```json
{
"success": true,
"data": {
"bucket_name": "my-bucket",
"object_key": "docs/file.txt",
"content_type": "text/plain",
"size": 1024,
"last_modified": "2024-01-01T00:00:00+00:00",
"etag": "\"abc123...\"",
"metadata": {
"author": "John Doe",
"project": "demo"
}
}
}
**Example:**```python
result = get_object_metadata("my-bucket", "docs/file.txt")
upload_object
Upload a file to S3.
Parameters:
- bucket_name (string, required): Name of the bucket
- object_key (string, required): Key for the object in S3
- file_path (string, required): Local file path to upload
- content_type (string, optional): MIME type of the file
- metadata (string, optional): Custom metadata as JSON string
Returns:```json
{
"success": true,
"data": {
"bucket_name": "my-bucket",
"object_key": "docs/file.txt",
"message": "Successfully uploaded object 'docs/file.txt' to bucket 'my-bucket'"
}
}
**Example:**```python
# Simple upload
result = upload_object("my-bucket", "docs/file.txt", "/tmp/file.txt")
# Upload with content type
result = upload_object("my-bucket", "docs/file.txt", "/tmp/file.txt",
content_type="text/plain")
# Upload with metadata
result = upload_object("my-bucket", "docs/file.txt", "/tmp/file.txt",
content_type="text/plain",
metadata='{"author": "John", "project": "demo"}')
Notes:
- The metadata parameter must be a valid JSON string representing a dictionary
- Custom metadata keys are automatically prefixed with x-amz-meta- by AWS
delete_object
Delete an object from S3.
Parameters:
- bucket_name (string, required): Name of the bucket
- object_key (string, required): Key of the object to delete
Returns:```json
{
"success": true,
"data": {
"bucket_name": "my-bucket",
"object_key": "docs/file.txt",
"message": "Successfully deleted object 'docs/file.txt' from bucket 'my-bucket'"
}
}
**Example:**```python
result = delete_object("my-bucket", "docs/file.txt")
Notes:
- This operation is idempotent - it succeeds even if the object doesn't exist
copy_object
Copy an object from one S3 location to another.
Parameters:
- source_bucket (string, required): Source bucket name
- source_key (string, required): Source object key
- dest_bucket (string, required): Destination bucket name
- dest_key (string, required): Destination object key
Returns:```json
{
"success": true,
"data": {
"source_bucket": "source-bucket",
"source_key": "source/file.txt",
"dest_bucket": "dest-bucket",
"dest_key": "dest/file.txt",
"message": "Successfully copied object from 'source-bucket/source/file.txt' to 'dest-bucket/dest/file.txt'"
}
}
**Example:**```python
# Copy within same bucket
result = copy_object("my-bucket", "docs/file.txt", "my-bucket", "archive/file.txt")
# Copy to different bucket
result = copy_object("my-bucket", "docs/file.txt", "backup-bucket", "file.txt")
Notes:
- All metadata from the source object is preserved in the copy
- The source object remains unchanged
move_object
Move an object from one S3 location to another.
Parameters:
- source_bucket (string, required): Source bucket name
- source_key (string, required): Source object key
- dest_bucket (string, required): Destination bucket name
- dest_key (string, required): Destination object key
Returns:```json
{
"success": true,
"data": {
"source_bucket": "source-bucket",
"source_key": "source/file.txt",
"dest_bucket": "dest-bucket",
"dest_key": "dest/file.txt",
"message": "Successfully moved object from 'source-bucket/source/file.txt' to 'dest-bucket/dest/file.txt'"
}
}
**Example:**```python
# Move within same bucket
result = move_object("my-bucket", "temp/file.txt", "my-bucket", "archive/file.txt")
# Move to different bucket
result = move_object("my-bucket", "temp/file.txt", "backup-bucket", "file.txt")
Notes:
- This operation is atomic: if the copy fails, the source object remains unchanged
- The move is implemented as a copy followed by a delete
Access Control
generate_presigned_download_url
Generate a presigned URL for downloading an S3 object.
Parameters:
- bucket_name (string, required): Name of the bucket
- object_key (string, required): Key of the object
- expiration (integer, optional): URL expiration time in seconds (default: 3600)
Returns:```json
{
"success": true,
"data": {
"bucket_name": "my-bucket",
"object_key": "docs/file.txt",
"presigned_url": "https://my-bucket.s3.amazonaws.com/docs/file.txt?...",
"expiration_seconds": 3600
}
}
**Example:**```python
# Generate URL with default expiration (1 hour)
result = generate_presigned_download_url("my-bucket", "docs/file.txt")
# Generate URL with custom expiration (2 hours)
result = generate_presigned_download_url("my-bucket", "docs/file.txt", expiration=7200)
Notes:
- The presigned URL grants temporary download access without requiring AWS credentials
- The URL expires after the specified duration
- Anyone with the URL can download the object until it expires
Metadata & Tagging
add_tags
Add or update tags on an S3 object.
Parameters:
- bucket_name (string, required): Name of the bucket
- object_key (string, required): Key of the object
- tags (string, required): Tags as JSON string (key-value pairs)
Returns:```json
{
"success": true,
"data": {
"bucket_name": "my-bucket",
"object_key": "docs/file.txt",
"tags_added": {
"project": "demo",
"env": "prod"
},
"message": "Successfully added tags to object 'docs/file.txt' in bucket 'my-bucket'"
}
}
**Example:**```python
# Add tags to an object
result = add_tags("my-bucket", "docs/file.txt",
'{"project": "demo", "env": "prod", "owner": "team-a"}')
Notes:
- If a tag key already exists, its value will be updated
- Existing tags not specified in the request are preserved
- The tags parameter must be a valid JSON string representing a dictionary
search_by_tag
Search for objects in a bucket by tag.
Parameters:
- bucket_name (string, required): Name of the bucket
- tag_key (string, required): Tag key to search for
- tag_value (string, required): Tag value to match
Returns:```json
{
"success": true,
"data": {
"bucket_name": "my-bucket",
"tag_key": "project",
"tag_value": "demo",
"matching_objects": [
{
"key": "docs/file1.txt",
"size": 1024
},
{
"key": "docs/file2.txt",
"size": 2048
}
],
"count": 2
}
}
**Example:**```python
# Search for objects with specific tag
result = search_by_tag("my-bucket", "project", "demo")
# Search for objects by environment
result = search_by_tag("my-bucket", "env", "prod")
Notes:
- This operation lists all objects in the bucket and checks their tags
- For large buckets, this operation may take some time
- Both the tag key and value must match exactly (case-sensitive)
Cost Analysis
calculate_bucket_storage_cost
Calculate the storage cost for an S3 bucket.
Parameters:
- bucket_name (string, required): Name of the bucket
- storage_class (string, optional): Storage class for pricing (default: "STANDARD")
- pricing_region (string, optional): AWS region for pricing (uses bucket region if not specified)
Supported Storage Classes:
- STANDARD - Standard storage (default)
- INTELLIGENT_TIERING - Intelligent-Tiering storage
- STANDARD_IA - Standard Infrequent Access
- ONEZONE_IA - One Zone Infrequent Access
- GLACIER - Glacier storage
- GLACIER_DEEP_ARCHIVE - Glacier Deep Archive
Returns:```json
{
"success": true,
"data": {
"bucket_name": "my-bucket",
"total_size_bytes": 1073741824,
"total_size_gb": 1.0,
"storage_class": "STANDARD",
"region": "us-east-1",
"price_per_gb": 0.023,
"estimated_monthly_cost_usd": 0.023
}
}
**Example:**```python
# Calculate cost with default storage class (STANDARD)
result = calculate_bucket_storage_cost("my-bucket")
# Calculate cost for Glacier storage
result = calculate_bucket_storage_cost("my-bucket", storage_class="GLACIER")
# Calculate cost for specific region
result = calculate_bucket_storage_cost("my-bucket",
storage_class="STANDARD_IA",
pricing_region="us-west-2")
Notes:
- The cost calculation is based on AWS S3 pricing as of the implementation date
- Actual costs may vary based on AWS pricing changes
- This calculation only includes storage costs, not request costs or data transfer costs
- For accurate billing, refer to your AWS billing dashboard
Error Handling
The server implements comprehensive error handling with structured error responses.
Common Error Types
| Error Code | Description | Example |
|---|---|---|
NoSuchBucket |
The specified bucket does not exist | Attempting to list objects in a non-existent bucket |
NoSuchKey |
The specified object does not exist | Attempting to get metadata for a non-existent object |
BucketAlreadyExists |
A bucket with this name already exists | Creating a bucket with a name that's already taken |
BucketNotEmpty |
Cannot delete bucket because it contains objects | Attempting to delete a bucket that has objects |
AccessDenied |
Insufficient permissions for the operation | Attempting an operation without proper IAM permissions |
InvalidBucketName |
The bucket name is invalid | Using invalid characters in bucket name |
FILE_NOT_FOUND |
Local file not found for upload | Attempting to upload a non-existent local file |
INVALID_METADATA |
Invalid metadata JSON format | Providing malformed JSON for metadata |
INVALID_TAGS |
Invalid tags JSON format | Providing malformed JSON for tags |
INVALID_STORAGE_CLASS |
Unsupported storage class | Using an unsupported storage class name |
Error Response Format
All errors follow this structure:```json
{
"success": false,
"error": {
"message": "Human-readable error description",
"code": "ERROR_CODE",
"type": "ErrorType"
}
}
### Example Error Responses
**Bucket Not Found:**```json
{
"success": false,
"error": {
"message": "Bucket 'non-existent-bucket' does not exist",
"code": "NoSuchBucket",
"type": "NoSuchBucket"
}
}
Bucket Not Empty:```json
{
"success": false,
"error": {
"message": "Cannot delete bucket 'my-bucket': bucket must be empty",
"code": "BucketNotEmpty",
"type": "BucketNotEmpty"
}
}
**Access Denied:**```json
{
"success": false,
"error": {
"message": "Access denied to bucket 'my-bucket'",
"code": "AccessDenied",
"type": "AccessDenied"
}
}
Troubleshooting
Server Won't Start
Problem: Server exits immediately with credential error
Solution:
1. Verify AWS credentials are configured:bash
aws configure list
2. Test credentials:bash
aws s3 ls
3. Check environment variables:bash
echo $AWS_ACCESS_KEY_ID
echo $AWS_SECRET_ACCESS_KEY
Permission Denied Errors
Problem: Operations fail with AccessDenied error
Solution:
1. Verify IAM permissions include required S3 actions
2. Check bucket policies don't deny access
3. Verify credentials have access to the specific bucket
4. Test with AWS CLI:bash
aws s3 ls s3://your-bucket-name
Bucket Creation Fails
Problem:
create_bucket fails with BucketAlreadyExists or InvalidBucketName
Solution:
1. Ensure bucket name is globally unique
2. Verify bucket name follows naming rules:
- 3-63 characters long
- Lowercase letters, numbers, and hyphens only
- Must start and end with letter or number
3. Try a different bucket name with a unique prefix
Object Upload Fails
Problem:
upload_object fails with FILE_NOT_FOUND error
Solution:
1. Verify the file path is correct and absolute
2. Check file exists:bash
ls -la /path/to/file
3. Verify read permissions on the file
Metadata/Tags JSON Errors
Problem:
upload_object or add_tags fails with INVALID_METADATA or INVALID_TAGS
Solution:
1. Ensure JSON is properly formatted
2. Use double quotes for keys and string values
3. Test JSON validity:python
import json
json.loads('{"key": "value"}') # Should not raise error
Cost Calculation Shows Zero
Problem:
calculate_bucket_storage_cost returns zero cost
Solution:
1. Verify the bucket contains objects:python
list_objects("my-bucket")
2. Check if the storage class is supported
3. Verify the region is supported in the pricing table
Logging Issues
Problem: Not seeing expected log output
Solution:
1. Check log level configuration
2. Ensure logging is configured before server starts
3. Check if logs are being written to a file instead of console
Connection Issues
Problem: MCP client cannot connect to server
Solution:
1. Verify server is running
2. Check server logs for startup errors
3. Verify MCP client configuration
4. Check network connectivity and firewall rules
Development
Setup Development Environment```bash
Clone the repository
git clone <repository-url>
cd aws-s3-mcp-server
Install development dependencies
pip install -e ".[dev]"
Run tests
pytest
Run tests with coverage
pytest --cov=src --cov-report=html
Format code
black src/ tests/
Lint code
ruff check src/ tests/
Type check
mypy src/
### Project Structure```
aws-s3-mcp-server/
├── src/
│ └── aws_s3_mcp_server/
│ ├── __init__.py
│ ├── server.py # Main server implementation
│ ├── cost_calculator.py # Cost calculation logic
│ ├── tools/
│ │ ├── __init__.py
│ │ ├── bucket_tools.py # Bucket management tools
│ │ └── object_tools.py # Object management tools
│ └── utils/
│ ├── __init__.py
│ ├── error_handler.py # Error handling utilities
│ └── logging_config.py # Logging configuration
├── tests/
│ ├── unit/ # Unit tests
│ ├── property/ # Property-based tests
│ └── integration/ # Integration tests
├── .kiro/
│ └── specs/ # Specification documents
├── pyproject.toml # Project configuration
├── requirements.txt # Dependencies
└── README.md # This file
Running Tests```bash
Run all tests
pytest
Run unit tests only
pytest tests/unit/
Run property-based tests only
pytest tests/property/
Run specific test file
pytest tests/unit/test_server_initialization.py
Run with verbose output
pytest -v
Run with coverage report
pytest --cov=src --cov-report=term-missing
Run specific test
pytest tests/unit/test_bucket_tools.py::test_list_buckets_success
```
Testing Strategy
The project uses a dual testing approach:
- Unit Tests: Verify specific examples and edge cases
- Property-Based Tests: Verify universal properties across randomized inputs using Hypothesis
All tests use the moto library to mock AWS S3 services, allowing tests to run without actual AWS credentials or infrastructure.
Logging
The server logs all operations with appropriate levels:
- INFO: Normal operations (tool invocations, successful operations)
- WARNING: Recoverable errors (object not found, bucket not empty)
- ERROR: Serious errors (credential failures, AWS API errors)
- DEBUG: Detailed information for troubleshooting
Important: Logs never contain sensitive information such as AWS credentials or file contents.
Contributing
Contributions are welcome! Please follow these guidelines:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Write tests for new functionality
- Ensure all tests pass (
pytest) - Format code (
black src/ tests/) - Lint code (
ruff check src/ tests/) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
MIT License - see LICENSE file for details
Support
For issues, questions, or contributions, please open an issue on the GitHub repository.
Acknowledgments
- Built with fastmcp framework
- Uses boto3 for AWS S3 interactions
- Property-based testing with Hypothesis
- AWS S3 mocking with moto
Additional Resources
- AWS S3 Documentation
- AWS S3 Pricing
- Model Context Protocol (MCP) Specification
- fastmcp Documentation
- boto3 S3 Documentation
