strava-activity-mcp-server 0.1.7__py3-none-any.whl → 0.1.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -31,192 +31,70 @@ def get_auth_url(client_id: int | None = None):
31
31
 
32
32
 
33
33
 
34
- @mcp.tool("strava://auth/token")
35
- def exchange_code_for_token(
34
+ @mcp.tool("strava://athlete/stats")
35
+ def get_athlete_stats(
36
36
  code: str,
37
37
  client_id: int | None = None,
38
- client_secret: str | None = None,
39
- ) -> dict:
40
- """Exchange an authorization code for access + refresh tokens."""
41
- if not code:
42
- return {"error": "authorization code is required"}
38
+ client_secret: str | None = None,) -> dict:
39
+
40
+ #'''Exchange an authorization code for access + refresh tokens.'''
41
+ if not code:
42
+ return {"error": "authorization code is required"}
43
43
 
44
- if client_id is None:
45
- client_id_env = os.getenv("STRAVA_CLIENT_ID")
46
- if not client_id_env:
47
- return {"error": "STRAVA_CLIENT_ID environment variable is not set"}
48
- try:
49
- client_id = int(client_id_env)
50
- except ValueError:
51
- return {"error": "STRAVA_CLIENT_ID must be an integer"}
44
+ if client_id is None:
45
+ client_id_env = os.getenv("STRAVA_CLIENT_ID")
46
+ if not client_id_env:
47
+ return {"error": "STRAVA_CLIENT_ID environment variable is not set"}
48
+ try:
49
+ client_id = int(client_id_env)
50
+ except ValueError:
51
+ return {"error": "STRAVA_CLIENT_ID must be an integer"}
52
52
 
53
- if client_secret is None:
54
- client_secret_env = os.getenv("STRAVA_CLIENT_SECRET")
55
- if not client_secret_env:
56
- return {"error": "STRAVA_CLIENT_SECRET environment variable is not set"}
57
- try:
58
- client_secret = str(client_secret_env)
59
- except ValueError:
60
- return {"error": "STRAVA_CLIENT_SECRET must be a string"}
53
+ if client_secret is None:
54
+ client_secret_env = os.getenv("STRAVA_CLIENT_SECRET")
55
+ if not client_secret_env:
56
+ return {"error": "STRAVA_CLIENT_SECRET environment variable is not set"}
57
+ try:
58
+ client_secret = str(client_secret_env)
59
+ except ValueError:
60
+ return {"error": "STRAVA_CLIENT_SECRET must be a string"}
61
61
 
62
- resp = requests.post(
63
- "https://www.strava.com/oauth/token",
64
- data={
62
+
63
+ resp = requests.post(
64
+ "https://www.strava.com/oauth/token",
65
+ data={
65
66
  "client_id": client_id,
66
67
  "client_secret": client_secret,
67
68
  "code": code,
68
69
  "grant_type": "authorization_code",
69
70
  },
70
71
  )
71
- try:
72
- resp.raise_for_status()
73
- except requests.HTTPError:
74
- return {"error": "token request failed", "status_code": resp.status_code, "response": resp.text}
72
+ try:
73
+ resp.raise_for_status()
74
+ except requests.HTTPError:
75
+ return {"error": "token request failed", "status_code": resp.status_code, "response": resp.text}
76
+ except Exception as e:
77
+ return {"error": "token request failed", "status_code": resp.status_code, "response": resp.text, "error": str(e)}
75
78
 
76
- tokens = resp.json()
79
+ tokens = resp.json()
77
80
  # Print tokens for debugging (optional)
78
- print(tokens)
81
+ print(tokens)
79
82
 
80
- access_token = tokens.get("access_token")
81
- refresh_token = tokens.get("refresh_token")
83
+ access_token = tokens.get("access_token")
84
+ refresh_token = tokens.get("refresh_token")
82
85
 
83
- return {"tokens": tokens, "access_token": access_token, "refresh_token": refresh_token}
84
86
 
87
+ #return {"tokens": tokens, "access_token": access_token, "refresh_token": refresh_token}
85
88
 
86
- def refresh_access_token(
87
- refresh_token: str,
88
- client_id: int,
89
- client_secret: str,
90
- ) -> dict:
91
- """Refresh an access token using a refresh token."""
92
- if not refresh_token:
93
- return {"error": "refresh_token is required"}
89
+ url = "https://www.strava.com/api/v3/athlete/activities?per_page=60"
90
+ headers = {
91
+ "accept": "application/json",
92
+ "authorization": f"Bearer {access_token}"
93
+ }
94
94
 
95
- resp = requests.post(
96
- "https://www.strava.com/oauth/token",
97
- data={
98
- "client_id": client_id,
99
- "client_secret": client_secret,
100
- "grant_type": "refresh_token",
101
- "refresh_token": refresh_token,
102
- },
103
- )
104
- try:
105
- resp.raise_for_status()
106
- except requests.HTTPError:
107
- return {"error": "refresh request failed", "status_code": resp.status_code, "response": resp.text}
108
-
109
- new_tokens = resp.json()
110
- # Print new tokens for debugging (optional)
111
- print(new_tokens)
112
- return new_tokens
113
-
114
-
115
- @mcp.tool("strava://athlete/stats")
116
- def _get_env_client_credentials() -> tuple[int | None, str | None]:
117
- """Read client id and secret from environment and return (client_id, client_secret).
118
-
119
- client_id will be returned as int if present and valid, otherwise None.
120
- """
121
- client_id = None
122
- client_secret = os.getenv("STRAVA_CLIENT_SECRET")
123
- client_id_env = os.getenv("STRAVA_CLIENT_ID")
124
- if client_id_env:
125
- try:
126
- client_id = int(client_id_env)
127
- except ValueError:
128
- client_id = None
129
- return client_id, client_secret
130
-
131
-
132
- def _ensure_access_token(token_or_tokens: object) -> tuple[str | None, dict | None]:
133
- """Given either an access token string or the token dict returned by the token endpoints,
134
- return a tuple (access_token, tokens_dict).
135
-
136
- If a dict is provided and contains no valid access_token but has a refresh_token,
137
- attempt to refresh using env client credentials. Returns (access_token, tokens_dict) or (None, None)
138
- on failure.
139
- """
140
- # If token_or_tokens is a string, assume it's an access token.
141
- if isinstance(token_or_tokens, str):
142
- return token_or_tokens, None
143
-
144
- # If it's a dict-like object, try to find access_token
145
- if isinstance(token_or_tokens, dict):
146
- access_token = token_or_tokens.get("access_token")
147
- if access_token:
148
- return access_token, token_or_tokens
149
-
150
- # try refresh flow
151
- refresh_token = token_or_tokens.get("refresh_token")
152
- client_id = token_or_tokens.get("client_id")
153
- client_secret = token_or_tokens.get("client_secret")
154
-
155
- # fallback to env vars if client id/secret not in the dict
156
- if not client_id or not client_secret:
157
- env_client_id, env_client_secret = _get_env_client_credentials()
158
- if not client_id:
159
- client_id = env_client_id
160
- if not client_secret:
161
- client_secret = env_client_secret
162
-
163
- if refresh_token and client_id and client_secret:
164
- try:
165
- new_tokens = refresh_access_token(refresh_token, int(client_id), client_secret)
166
- except Exception as e:
167
- print(f"refresh failed: {e}")
168
- return None, None
169
-
170
- access_token = new_tokens.get("access_token")
171
- return access_token, new_tokens
172
-
173
- return None, None
174
-
175
-
176
- @mcp.tool("strava://athlete/stats")
177
- def get_athlete_stats(token: object) -> object:
178
- """Retrieve athlete activities using either an access token string or a token dict.
179
-
180
- If a token dict is provided and the access token is missing/expired, the function will
181
- attempt to refresh it (one attempt) using provided refresh token and client credentials
182
- (falling back to STRAVA_CLIENT_ID/STRAVA_CLIENT_SECRET environment variables).
183
- """
184
- access_token, tokens_dict = _ensure_access_token(token)
185
- if not access_token:
186
- return {"error": "Could not obtain an access token"}
187
-
188
- url = "https://www.strava.com/api/v3/athlete/activities?per_page=60"
189
- headers = {
190
- "accept": "application/json",
191
- "authorization": f"Bearer {access_token}"
192
- }
95
+ response = requests.get(url, headers=headers)
193
96
 
194
- response = requests.get(url, headers=headers)
195
-
196
- # If unauthorized, try one refresh if we have a refresh token available
197
- if response.status_code == 401 and isinstance(token, dict):
198
- refresh_token = token.get("refresh_token") or (tokens_dict or {}).get("refresh_token")
199
- if refresh_token:
200
- client_id = token.get("client_id") or (tokens_dict or {}).get("client_id")
201
- client_secret = token.get("client_secret") or (tokens_dict or {}).get("client_secret")
202
- if not client_id or not client_secret:
203
- env_client_id, env_client_secret = _get_env_client_credentials()
204
- client_id = client_id or env_client_id
205
- client_secret = client_secret or env_client_secret
206
-
207
- if client_id and client_secret:
208
- new_tokens = refresh_access_token(refresh_token, int(client_id), client_secret)
209
- new_access = new_tokens.get("access_token")
210
- if new_access:
211
- headers["authorization"] = f"Bearer {new_access}"
212
- response = requests.get(url, headers=headers)
213
-
214
- try:
215
- response.raise_for_status()
216
- except requests.HTTPError:
217
- return {"error": "request failed", "status_code": response.status_code, "response": response.text}
218
-
219
- return response.json()
97
+ return response.json()
220
98
 
221
99
  if __name__ == "__main__":
222
100
  mcp.run(transport="stdio") # Run the server, using standard input/output for communication
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: strava-activity-mcp-server
3
- Version: 0.1.7
4
- Summary: Trying to implement environment variables for client_id
3
+ Version: 0.1.8
4
+ Summary: STRAVA ACTIVITY MCP SERVER
5
5
  License-File: LICENSE
6
6
  Requires-Python: >=3.10
7
7
  Requires-Dist: build>=1.3.0
@@ -0,0 +1,8 @@
1
+ strava_activity_mcp_server/__init__.py,sha256=jaa1ZVuEJMwuVGzj67oqC_ESUUiwblVVH-NEtTiQQdQ,110
2
+ strava_activity_mcp_server/__main__.py,sha256=SAdVoObdjb5UP4MY-Y2_uCXpnthB6hgxlb1KNVNgOrc,58
3
+ strava_activity_mcp_server/strava_activity_mcp_server.py,sha256=lfgnxF8w_NBl_NRwYcL2stGedwvKkMkKbwZy7TK0L74,3729
4
+ strava_activity_mcp_server-0.1.8.dist-info/METADATA,sha256=IYzmmg30BrbrlZT0WBFxbuDA8cq_lAeTQv9yQxXDXsQ,5553
5
+ strava_activity_mcp_server-0.1.8.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
6
+ strava_activity_mcp_server-0.1.8.dist-info/entry_points.txt,sha256=F6PO_DBSThhtmX2AC-tu2MIiCJkGi31LCaQJxfUzZ5g,79
7
+ strava_activity_mcp_server-0.1.8.dist-info/licenses/LICENSE,sha256=IwGE9guuL-ryRPEKi6wFPI_zOhg7zDZbTYuHbSt_SAk,35823
8
+ strava_activity_mcp_server-0.1.8.dist-info/RECORD,,
@@ -1,8 +0,0 @@
1
- strava_activity_mcp_server/__init__.py,sha256=jaa1ZVuEJMwuVGzj67oqC_ESUUiwblVVH-NEtTiQQdQ,110
2
- strava_activity_mcp_server/__main__.py,sha256=SAdVoObdjb5UP4MY-Y2_uCXpnthB6hgxlb1KNVNgOrc,58
3
- strava_activity_mcp_server/strava_activity_mcp_server.py,sha256=FenliPAzC8OyFVReM2iOofrbiar8eD6YW5Il4Ee851M,8567
4
- strava_activity_mcp_server-0.1.7.dist-info/METADATA,sha256=mZ0TKn5ODR2CdNwg_VJDrndBGd_LBgRJpprR0Rh-i1A,5582
5
- strava_activity_mcp_server-0.1.7.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
6
- strava_activity_mcp_server-0.1.7.dist-info/entry_points.txt,sha256=F6PO_DBSThhtmX2AC-tu2MIiCJkGi31LCaQJxfUzZ5g,79
7
- strava_activity_mcp_server-0.1.7.dist-info/licenses/LICENSE,sha256=IwGE9guuL-ryRPEKi6wFPI_zOhg7zDZbTYuHbSt_SAk,35823
8
- strava_activity_mcp_server-0.1.7.dist-info/RECORD,,