nate nowack commited on
Commit
161e1b2
·
unverified ·
2 Parent(s): 68ec05766e899b

Merge pull request #29 from jlowin/surge-example

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