Jeremiah Lowin commited on
Commit
8a40e1f
·
1 Parent(s): 5696158

Add anthropic guide

Browse files
docs/docs.json CHANGED
@@ -97,6 +97,7 @@
97
  "group": "Integrations",
98
  "pages": [
99
  "integrations/openai",
 
100
  "integrations/contrib"
101
  ]
102
  },
 
97
  "group": "Integrations",
98
  "pages": [
99
  "integrations/openai",
100
+ "integrations/anthropic",
101
  "integrations/contrib"
102
  ]
103
  },
docs/integrations/anthropic.mdx ADDED
@@ -0,0 +1,226 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Anthropic
3
+ sidebarTitle: Anthropic
4
+ description: Integrate FastMCP servers with the Anthropic Messages API
5
+ icon: message-smile
6
+ ---
7
+
8
+ import { VersionBadge } from "/snippets/version-badge.mdx"
9
+
10
+ Anthropic's Claude supports MCP servers through the MCP connector feature in the Messages API, allowing you to extend AI capabilities with custom tools from remote MCP servers.
11
+
12
+ ## Messages API
13
+
14
+ Anthropic's [Messages API](https://docs.anthropic.com/en/api/messages) supports [MCP servers](https://docs.anthropic.com/en/docs/agents-and-tools/mcp-connector) as remote tool sources through the MCP connector feature.
15
+
16
+
17
+ <Tip>
18
+ Currently, the MCP connector only accesses **tools** from MCP servers—it queries the `list_tools` endpoint and exposes those functions to Claude. Other MCP features like resources and prompts are not currently supported.
19
+ </Tip>
20
+
21
+ ### Create a Server
22
+
23
+ First, create a FastMCP server with the tools you want to expose. For this example, we'll create a server with a single tool that rolls dice.
24
+
25
+ ```python server.py
26
+ import random
27
+ from fastmcp import FastMCP
28
+
29
+ mcp = FastMCP(name="Dice Roller")
30
+
31
+ @mcp.tool()
32
+ def roll_dice(n_dice: int) -> list[int]:
33
+ """Roll `n_dice` 6-sided dice and return the results."""
34
+ return [random.randint(1, 6) for _ in range(n_dice)]
35
+
36
+ if __name__ == "__main__":
37
+ mcp.run(transport="sse", port=8000)
38
+ ```
39
+
40
+ ### Deploy the Server
41
+
42
+ Your server must be deployed to a public URL in order for Anthropic to access it. The MCP connector supports both SSE and Streamable HTTP transports.
43
+
44
+ For development, you can use tools like `ngrok` to temporarily expose a locally-running server to the internet. We'll do that for this example (you may need to install `ngrok` and create a free account), but you can use any other method to deploy your server.
45
+
46
+ Assuming you saved the above code as `server.py`, you can run the following two commands in two separate terminals to deploy your server and expose it to the internet:
47
+
48
+ <CodeGroup>
49
+ ```bash FastMCP server
50
+ python server.py
51
+ ```
52
+
53
+ ```bash ngrok
54
+ ngrok http 8000
55
+ ```
56
+ </CodeGroup>
57
+
58
+ <Warning>
59
+ This exposes your unauthenticated server to the internet. Only run this command in a safe environment if you understand the risks.
60
+ </Warning>
61
+
62
+ ### Call the Server
63
+
64
+ To use the Messages API with MCP servers, you'll need to install the Anthropic Python SDK (not included with FastMCP):
65
+
66
+ ```bash
67
+ pip install anthropic
68
+ ```
69
+
70
+ Here is an example of how to call your server from Python. Note that you'll need to replace `https://your-server-url.com` with the actual URL of your server. In addition, we use `/sse` as the endpoint because we deployed an SSE server with the default path; you may need to use a different endpoint if you customized your server's deployment.
71
+
72
+ ```python {4, 12-19}
73
+ import anthropic
74
+ from rich import print
75
+
76
+ # Your server URL (replace with your actual URL)
77
+ url = 'https://your-server-url.com'
78
+
79
+ client = anthropic.Anthropic()
80
+
81
+ response = client.beta.messages.create(
82
+ model="claude-sonnet-4-20250514",
83
+ max_tokens=1000,
84
+ messages=[{"role": "user", "content": "Roll a few dice!"}],
85
+ mcp_servers=[
86
+ {
87
+ "type": "url",
88
+ "url": f"{url}/sse",
89
+ "name": "dice-server",
90
+ }
91
+ ],
92
+ extra_headers={
93
+ "anthropic-beta": "mcp-client-2025-04-04"
94
+ }
95
+ )
96
+
97
+ print(response.content)
98
+ ```
99
+
100
+ If you run this code, you'll see something like the following output:
101
+
102
+ ```text
103
+ I'll roll some dice for you! Let me use the dice rolling tool.
104
+
105
+ I rolled 3 dice and got: 4, 2, 6
106
+
107
+ The results were 4, 2, and 6. Would you like me to roll again or roll a different number of dice?
108
+ ```
109
+
110
+
111
+ ### Authentication
112
+
113
+ <VersionBadge version="2.6.0" />
114
+
115
+ The MCP connector supports OAuth authentication through authorization tokens, which means you can secure your server while still allowing Anthropic to access it.
116
+
117
+ #### Server Authentication
118
+
119
+ The simplest way to add authentication to the server is to use a bearer token scheme.
120
+
121
+ For this example, we'll quickly generate our own tokens with FastMCP's `RSAKeyPair` utility, but this may not be appropriate for production use. For more details, see the complete server-side [Bearer Auth](/servers/auth/bearer) documentation.
122
+
123
+ We'll start by creating an RSA key pair to sign and verify tokens.
124
+
125
+ ```python
126
+ from fastmcp.server.auth.providers.bearer import RSAKeyPair
127
+
128
+ key_pair = RSAKeyPair.generate()
129
+ access_token = key_pair.create_token(audience="dice-server")
130
+ ```
131
+
132
+ <Warning>
133
+ FastMCP's `RSAKeyPair` utility is for development and testing only.
134
+ </Warning>
135
+
136
+ Next, we'll create a `BearerAuthProvider` to authenticate the server.
137
+
138
+ ```python
139
+ from fastmcp import FastMCP
140
+ from fastmcp.server.auth import BearerAuthProvider
141
+
142
+ auth = BearerAuthProvider(
143
+ public_key=key_pair.public_key,
144
+ audience="dice-server",
145
+ )
146
+
147
+ mcp = FastMCP(name="Dice Roller", auth=auth)
148
+ ```
149
+
150
+ Here is a complete example that you can copy/paste. For simplicity and the purposes of this example only, it will print the token to the console. **Do NOT do this in production!**
151
+
152
+ ```python server.py [expandable]
153
+ from fastmcp import FastMCP
154
+ from fastmcp.server.auth import BearerAuthProvider
155
+ from fastmcp.server.auth.providers.bearer import RSAKeyPair
156
+ import random
157
+
158
+ key_pair = RSAKeyPair.generate()
159
+ access_token = key_pair.create_token(audience="dice-server")
160
+
161
+ auth = BearerAuthProvider(
162
+ public_key=key_pair.public_key,
163
+ audience="dice-server",
164
+ )
165
+
166
+ mcp = FastMCP(name="Dice Roller", auth=auth)
167
+
168
+ @mcp.tool()
169
+ def roll_dice(n_dice: int) -> list[int]:
170
+ """Roll `n_dice` 6-sided dice and return the results."""
171
+ return [random.randint(1, 6) for _ in range(n_dice)]
172
+
173
+ if __name__ == "__main__":
174
+ print(f"\n---\n\n🔑 Dice Roller access token:\n\n{access_token}\n\n---\n")
175
+ mcp.run(transport="sse", port=8000)
176
+ ```
177
+
178
+ #### Client Authentication
179
+
180
+ If you try to call the authenticated server with the same Anthropic code we wrote earlier, you'll get an error indicating that the server rejected the request because it's not authenticated.
181
+
182
+ ```python
183
+ Error code: 400 - {
184
+ "type": "error",
185
+ "error": {
186
+ "type": "invalid_request_error",
187
+ "message": "MCP server 'dice-server' requires authentication. Please provide an authorization_token.",
188
+ },
189
+ }
190
+ ```
191
+
192
+ To authenticate the client, you can pass the token using the `authorization_token` parameter in your MCP server configuration:
193
+
194
+ ```python {7, 17}
195
+ import anthropic
196
+ from rich import print
197
+
198
+ # Your server URL (replace with your actual URL)
199
+ url = 'https://your-server-url.com'
200
+
201
+ # Your access token (replace with your actual token)
202
+ access_token = 'your-access-token'
203
+
204
+ client = anthropic.Anthropic()
205
+
206
+ response = client.beta.messages.create(
207
+ model="claude-sonnet-4-20250514",
208
+ max_tokens=1000,
209
+ messages=[{"role": "user", "content": "Roll a few dice!"}],
210
+ mcp_servers=[
211
+ {
212
+ "type": "url",
213
+ "url": f"{url}/sse",
214
+ "name": "dice-server",
215
+ "authorization_token": access_token
216
+ }
217
+ ],
218
+ extra_headers={
219
+ "anthropic-beta": "mcp-client-2025-04-04"
220
+ }
221
+ )
222
+
223
+ print(response.content)
224
+ ```
225
+
226
+ You should now see the dice roll results in the output.
docs/integrations/openai.mdx CHANGED
@@ -123,7 +123,9 @@ key_pair = RSAKeyPair.generate()
123
  access_token = key_pair.create_token(audience="dice-server")
124
  ```
125
 
126
- This will generate a new RSA key pair and a corresponding access token.
 
 
127
 
128
  Next, we'll create a `BearerAuthProvider` to authenticate the server.
129
 
 
123
  access_token = key_pair.create_token(audience="dice-server")
124
  ```
125
 
126
+ <Warning>
127
+ FastMCP's `RSAKeyPair` utility is for development and testing only.
128
+ </Warning>
129
 
130
  Next, we'll create a `BearerAuthProvider` to authenticate the server.
131
 
server.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+
3
+ from fastmcp import FastMCP
4
+ from fastmcp.server.auth import BearerAuthProvider
5
+ from fastmcp.server.auth.providers.bearer import RSAKeyPair
6
+
7
+ key_pair = RSAKeyPair.generate()
8
+ access_token = key_pair.create_token(audience="dice-server")
9
+
10
+ auth = BearerAuthProvider(
11
+ public_key=key_pair.public_key,
12
+ audience="dice-server",
13
+ )
14
+
15
+ mcp = FastMCP(name="Dice Roller", auth=auth)
16
+
17
+
18
+ @mcp.tool()
19
+ def roll_dice(n_dice: int) -> list[int]:
20
+ """Roll `n_dice` 6-sided dice and return the results."""
21
+ return [random.randint(1, 6) for _ in range(n_dice)]
22
+
23
+
24
+ if __name__ == "__main__":
25
+ print(f"\n---\n\n🔑 Dice Roller access token:\n\n{access_token}\n\n---\n")
26
+ mcp.run(transport="sse", port=8000)