|
1 | 1 | import json |
2 | 2 | import os |
| 3 | +from urllib.parse import urlparse |
3 | 4 |
|
4 | 5 | from mcp.server.fastmcp import FastMCP |
5 | 6 | from requests_oauthlib import OAuth1Session |
@@ -45,6 +46,66 @@ def get_hours_of_operation(location_id: int, hours_type: str = 'primary') -> lis |
45 | 46 | return content['hours_by_type'].get(hours_type, []) |
46 | 47 |
|
47 | 48 |
|
| 49 | +@mcp.tool() |
| 50 | +def get_businesses() -> list: |
| 51 | + """Get all businesses within the DevHub account |
| 52 | +
|
| 53 | + Returns a list of businesses with the following fields: |
| 54 | + - id: Business ID that can be used in the other tools |
| 55 | + - business_name: Business name |
| 56 | +
|
| 57 | + If only one business exists in the account, you can assume that the user wants to use that business for any business_id related tools. |
| 58 | + """ |
| 59 | + client, base_url = get_client() |
| 60 | + params = { |
| 61 | + 'deleted': 0, |
| 62 | + 'limit': 20, |
| 63 | + 'order_by': 'business_name', |
| 64 | + 'project_type': 'default', |
| 65 | + } |
| 66 | + r = client.get('{}businesses/'.format(base_url), params=params) |
| 67 | + content = json.loads(r.content) |
| 68 | + return content['objects'] |
| 69 | + |
| 70 | + |
| 71 | +@mcp.tool() |
| 72 | +def get_locations(business_id: int) -> list: |
| 73 | + """Get all locations for a business |
| 74 | +
|
| 75 | + Returns a list of locations with the following fields: |
| 76 | + - id: Location ID that can be used in the other tools |
| 77 | + - location_name: Location name |
| 78 | + - location_url: Location URL in DevHub |
| 79 | + - street: Street address |
| 80 | + - city: City |
| 81 | + - state: State |
| 82 | + - country: Country |
| 83 | + - postal_code: Postal code |
| 84 | + - lat: Latitude |
| 85 | + - lon: Longitude |
| 86 | + """ |
| 87 | + client, base_url = get_client() |
| 88 | + params = { |
| 89 | + 'business_id': business_id, |
| 90 | + 'limit': 600, |
| 91 | + 'order_by': 'location_name', |
| 92 | + } |
| 93 | + r = client.get('{}locations/'.format(base_url), params=params) |
| 94 | + content = json.loads(r.content) |
| 95 | + return [{ |
| 96 | + 'id': location['id'], |
| 97 | + 'location_name': location['location_name'], |
| 98 | + 'location_url': location['location_url'], |
| 99 | + 'street': location['street'], |
| 100 | + 'city': location['city'], |
| 101 | + 'state': location['state'], |
| 102 | + 'country': location['country'], |
| 103 | + 'postal_code': location['postal_code'], |
| 104 | + 'lat': location['lat'], |
| 105 | + 'lon': location['lon'], |
| 106 | + } for location in content['objects']] |
| 107 | + |
| 108 | + |
48 | 109 | @mcp.tool() |
49 | 110 | def update_hours(location_id: int, new_hours: list, hours_type: str = 'primary') -> str: |
50 | 111 | """Update the hours of operation for a DevHub location |
@@ -81,6 +142,42 @@ def update_hours(location_id: int, new_hours: list, hours_type: str = 'primary') |
81 | 142 | return 'Updated successfully' |
82 | 143 |
|
83 | 144 |
|
| 145 | +@mcp.tool() |
| 146 | +def site_from_url(url: str) -> str: |
| 147 | + """Get the DevHub site ID from a URL. |
| 148 | +
|
| 149 | + Can prompt the user for the URL instead of passing a site_id. |
| 150 | +
|
| 151 | + Returns details about the Site matches the URL that can be used in the other tools. |
| 152 | + - Site ID: ID of the DevHub site |
| 153 | + - Site URL: URL of the DevHub site |
| 154 | + - Site Location IDs: List of location IDs associated with the site |
| 155 | +
|
| 156 | + Args: |
| 157 | + url: URL of the DevHub site, all lowercase and ends with a slash |
| 158 | + """ |
| 159 | + parsed_url = urlparse(url) |
| 160 | + subdomain = parsed_url.netloc.split('.', 1)[0] or 'www' |
| 161 | + domain = parsed_url.netloc.split('.', 1)[1] |
| 162 | + base_directory = parsed_url.path |
| 163 | + client, base_url = get_client() |
| 164 | + r = client.get('{}sites/'.format(base_url), params={ |
| 165 | + 'base_directory': base_directory, |
| 166 | + 'deleted': 0, |
| 167 | + 'domain': domain, |
| 168 | + 'subdomain': subdomain, |
| 169 | + }) |
| 170 | + content = json.loads(r.content) |
| 171 | + if len(content['objects']) == 0: |
| 172 | + return 'No site found' |
| 173 | + site = content['objects'][0] |
| 174 | + return f""" |
| 175 | +Site ID: {site['id']} |
| 176 | +Site URL: {site['formatted_url']} |
| 177 | +Site Location IDs: {", ".join([str(location_id) for location_id in site['location_ids']])} |
| 178 | +""" |
| 179 | + |
| 180 | + |
84 | 181 | @mcp.tool() |
85 | 182 | def upload_image(base64_image_content: str, filename: str) -> str: |
86 | 183 | """Upload an image to the DevHub media gallery |
|
0 commit comments