1
1
"""
2
2
SageMaker Unified Studio Project Context MCP Server in stdio transport.
3
3
4
- This is a self-contained MCP Server, ready to be used directly.
5
- Dependencies:
6
- pip install mcp[cli]
7
- pip install sagemaker_studio
8
4
"""
9
5
10
6
import json
11
7
import logging
12
8
import os
13
9
from typing import Any , Dict
14
10
15
- import boto3
16
11
from mcp .server .fastmcp import FastMCP
17
- from sagemaker_studio import ClientConfig , Project
18
12
19
13
# Configure logging
20
14
logging .basicConfig (level = logging .INFO )
@@ -43,15 +37,16 @@ def __init__(self):
43
37
self .domain_id = metadata ["AdditionalMetadata" ]["DataZoneDomainId" ]
44
38
self .project_id = metadata ["AdditionalMetadata" ]["DataZoneProjectId" ]
45
39
self .region = metadata ["AdditionalMetadata" ]["DataZoneDomainRegion" ]
46
-
47
- logger .info (f"Read self.domain: { self .domain_id } " )
48
-
49
- self .session = boto3 .Session (profile_name = "DomainExecutionRoleCreds" , region_name = self .region )
50
- client_conf = ClientConfig (session = self .session , region = self .region )
51
- self .project = Project (id = self .project_id , domain_id = self .domain_id , config = client_conf )
52
40
except Exception as e :
53
41
raise RuntimeError (f"Failed to initialize project: { e } " )
54
42
43
+ if not self .domain_id :
44
+ raise RuntimeError (f"Domain id should not be empty" )
45
+ if not self .project_id :
46
+ raise RuntimeError (f"Project id should not be empty" )
47
+ if not self .region :
48
+ raise RuntimeError (f"Region should not be empty" )
49
+
55
50
56
51
def safe_get_attr (obj : Any , attr : str , default : Any = None ) -> Any :
57
52
"""Safely get an attribute from an object."""
@@ -66,12 +61,12 @@ def safe_get_attr(obj: Any, attr: str, default: Any = None) -> Any:
66
61
try :
67
62
return value ()
68
63
except (RuntimeError , Exception ) as e :
69
- logger .debug (f"Error calling attribute { attr } : { e } " )
64
+ logger .error (f"Error calling attribute { attr } : { e } " )
70
65
return default
71
66
return value
72
67
return default
73
68
except Exception as e :
74
- logger .debug (f"Error getting attribute { attr } : { e } " )
69
+ logger .error (f"Error getting attribute { attr } : { e } " )
75
70
return default
76
71
77
72
@@ -99,7 +94,6 @@ async def aws_context_provider() -> Dict[str, Any]:
99
94
- domain identifier: Unique identifier for the AWS DataZone domain
100
95
- project identifier: Identifier for the specific project being worked on
101
96
- profile name: Name of the AWS profile to use for credentials
102
- - project profile connection name: Connection name for project integration
103
97
- region: AWS region where operations should be performed
104
98
- aws profiles: use the aws profile named DomainExecutionRoleCreds for calling datazone APIs; otherwise use default AWS profile
105
99
@@ -116,7 +110,7 @@ async def aws_context_provider() -> Dict[str, Any]:
116
110
return {"response" : identifiers_response }
117
111
except Exception as e :
118
112
logger .error (f"Error providing SMUS context identifiers: { e } " )
119
- return {"response" : identifiers_response , "error" : str ( e ) }
113
+ return {"response" : identifiers_response , "error" : "Error providing SMUS context identifiers" }
120
114
121
115
122
116
if __name__ == "__main__" :
0 commit comments