|
| 1 | +# /// script |
| 2 | +# dependencies = [] |
| 3 | +# /// |
| 4 | + |
| 5 | +"""MCPServer Text Me Server |
| 6 | +-------------------------------- |
| 7 | +This defines a simple MCPServer server that sends a text message to a phone number via https://surgemsg.com/. |
| 8 | +
|
| 9 | +To run this example, create a `.env` file with the following values: |
| 10 | +
|
| 11 | +SURGE_API_KEY=... |
| 12 | +SURGE_ACCOUNT_ID=... |
| 13 | +SURGE_MY_PHONE_NUMBER=... |
| 14 | +SURGE_MY_FIRST_NAME=... |
| 15 | +SURGE_MY_LAST_NAME=... |
| 16 | +
|
| 17 | +Visit https://surgemsg.com/ and click "Get Started" to obtain these values. |
| 18 | +""" |
| 19 | + |
| 20 | +from typing import Annotated |
| 21 | + |
| 22 | +import httpx2 |
| 23 | +from pydantic import BeforeValidator |
| 24 | +from pydantic_settings import BaseSettings, SettingsConfigDict |
| 25 | + |
| 26 | +from mcp.server.mcpserver import MCPServer |
| 27 | + |
| 28 | + |
| 29 | +class SurgeSettings(BaseSettings): |
| 30 | + model_config: SettingsConfigDict = SettingsConfigDict(env_prefix="SURGE_", env_file=".env") |
| 31 | + |
| 32 | + api_key: str |
| 33 | + account_id: str |
| 34 | + my_phone_number: Annotated[str, BeforeValidator(lambda v: "+" + v if not v.startswith("+") else v)] |
| 35 | + my_first_name: str |
| 36 | + my_last_name: str |
| 37 | + |
| 38 | + |
| 39 | +# Create server |
| 40 | +mcp = MCPServer("Text me") |
| 41 | +surge_settings = SurgeSettings() # type: ignore |
| 42 | + |
| 43 | + |
| 44 | +@mcp.tool(name="textme", description="Send a text message to me") |
| 45 | +def text_me(text_content: str) -> str: |
| 46 | + """Send a text message to a phone number via https://surgemsg.com/""" |
| 47 | + with httpx2.Client() as client: |
| 48 | + response = client.post( |
| 49 | + "https://api.surgemsg.com/messages", |
| 50 | + headers={ |
| 51 | + "Authorization": f"Bearer {surge_settings.api_key}", |
| 52 | + "Surge-Account": surge_settings.account_id, |
| 53 | + "Content-Type": "application/json", |
| 54 | + }, |
| 55 | + json={ |
| 56 | + "body": text_content, |
| 57 | + "conversation": { |
| 58 | + "contact": { |
| 59 | + "first_name": surge_settings.my_first_name, |
| 60 | + "last_name": surge_settings.my_last_name, |
| 61 | + "phone_number": surge_settings.my_phone_number, |
| 62 | + } |
| 63 | + }, |
| 64 | + }, |
| 65 | + ) |
| 66 | + response.raise_for_status() |
| 67 | + return f"Message sent: {text_content}" |
0 commit comments