fastmcp 0.3.5__tar.gz → 0.4.1__tar.gz

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.
Files changed (65) hide show
  1. {fastmcp-0.3.5 → fastmcp-0.4.1}/.github/ai-labeler.yml +3 -4
  2. fastmcp-0.3.5/.github/workflows/lint.yml → fastmcp-0.4.1/.github/workflows/run-static.yml +6 -0
  3. {fastmcp-0.3.5 → fastmcp-0.4.1}/.gitignore +1 -1
  4. fastmcp-0.4.1/LICENSE +21 -0
  5. {fastmcp-0.3.5 → fastmcp-0.4.1}/PKG-INFO +6 -2
  6. fastmcp-0.4.1/examples/memory.py +346 -0
  7. {fastmcp-0.3.5 → fastmcp-0.4.1}/pyproject.toml +15 -1
  8. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/cli/cli.py +9 -3
  9. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/prompts/base.py +29 -13
  10. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/resources/base.py +5 -13
  11. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/resources/templates.py +1 -1
  12. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/resources/types.py +10 -1
  13. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/server.py +22 -14
  14. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/utilities/func_metadata.py +11 -11
  15. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/utilities/logging.py +4 -1
  16. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/utilities/types.py +3 -1
  17. {fastmcp-0.3.5 → fastmcp-0.4.1}/tests/prompts/test_base.py +7 -6
  18. {fastmcp-0.3.5 → fastmcp-0.4.1}/tests/resources/test_file_resources.py +9 -8
  19. {fastmcp-0.3.5 → fastmcp-0.4.1}/tests/resources/test_function_resources.py +9 -8
  20. {fastmcp-0.3.5 → fastmcp-0.4.1}/tests/resources/test_resource_manager.py +10 -9
  21. fastmcp-0.4.1/tests/resources/test_resource_template.py +181 -0
  22. {fastmcp-0.3.5 → fastmcp-0.4.1}/tests/resources/test_resources.py +9 -8
  23. fastmcp-0.4.1/tests/servers/__init__.py +0 -0
  24. {fastmcp-0.3.5 → fastmcp-0.4.1}/tests/servers/test_file_server.py +4 -2
  25. {fastmcp-0.3.5 → fastmcp-0.4.1}/tests/test_cli.py +53 -39
  26. {fastmcp-0.3.5 → fastmcp-0.4.1}/tests/test_func_metadata.py +36 -19
  27. {fastmcp-0.3.5 → fastmcp-0.4.1}/tests/test_server.py +143 -80
  28. {fastmcp-0.3.5 → fastmcp-0.4.1}/uv.lock +46 -1
  29. fastmcp-0.3.5/LICENSE +0 -201
  30. fastmcp-0.3.5/tests/resources/test_resource_template.py +0 -238
  31. {fastmcp-0.3.5 → fastmcp-0.4.1}/.github/release.yml +0 -0
  32. {fastmcp-0.3.5 → fastmcp-0.4.1}/.github/workflows/ai-labeler.yml +0 -0
  33. {fastmcp-0.3.5 → fastmcp-0.4.1}/.github/workflows/publish.yml +0 -0
  34. {fastmcp-0.3.5 → fastmcp-0.4.1}/.github/workflows/run-tests.yml +0 -0
  35. {fastmcp-0.3.5 → fastmcp-0.4.1}/.pre-commit-config.yaml +0 -0
  36. {fastmcp-0.3.5 → fastmcp-0.4.1}/.python-version +0 -0
  37. {fastmcp-0.3.5 → fastmcp-0.4.1}/README.md +0 -0
  38. {fastmcp-0.3.5 → fastmcp-0.4.1}/Windows_Notes.md +0 -0
  39. {fastmcp-0.3.5 → fastmcp-0.4.1}/docs/assets/demo-inspector.png +0 -0
  40. {fastmcp-0.3.5 → fastmcp-0.4.1}/examples/complex_inputs.py +0 -0
  41. {fastmcp-0.3.5 → fastmcp-0.4.1}/examples/desktop.py +0 -0
  42. {fastmcp-0.3.5 → fastmcp-0.4.1}/examples/echo.py +0 -0
  43. {fastmcp-0.3.5 → fastmcp-0.4.1}/examples/readme-quickstart.py +0 -0
  44. {fastmcp-0.3.5 → fastmcp-0.4.1}/examples/screenshot.py +0 -0
  45. {fastmcp-0.3.5 → fastmcp-0.4.1}/examples/simple_echo.py +0 -0
  46. {fastmcp-0.3.5 → fastmcp-0.4.1}/examples/text_me.py +0 -0
  47. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/__init__.py +0 -0
  48. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/cli/__init__.py +0 -0
  49. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/cli/claude.py +0 -0
  50. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/exceptions.py +0 -0
  51. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/prompts/__init__.py +0 -0
  52. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/prompts/manager.py +0 -0
  53. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/prompts/prompt_manager.py +0 -0
  54. /fastmcp-0.3.5/tests/__init__.py → /fastmcp-0.4.1/src/fastmcp/py.typed +0 -0
  55. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/resources/__init__.py +0 -0
  56. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/resources/resource_manager.py +0 -0
  57. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/tools/__init__.py +0 -0
  58. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/tools/base.py +0 -0
  59. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/tools/tool_manager.py +0 -0
  60. {fastmcp-0.3.5 → fastmcp-0.4.1}/src/fastmcp/utilities/__init__.py +0 -0
  61. {fastmcp-0.3.5/tests/prompts → fastmcp-0.4.1/tests}/__init__.py +0 -0
  62. {fastmcp-0.3.5/tests/resources → fastmcp-0.4.1/tests/prompts}/__init__.py +0 -0
  63. {fastmcp-0.3.5 → fastmcp-0.4.1}/tests/prompts/test_manager.py +0 -0
  64. {fastmcp-0.3.5/tests/servers → fastmcp-0.4.1/tests/resources}/__init__.py +0 -0
  65. {fastmcp-0.3.5 → fastmcp-0.4.1}/tests/test_tool_manager.py +0 -0
@@ -1,6 +1,6 @@
1
1
  instructions: |
2
2
  Apply the minimal set of labels that accurately characterize the issue/PR:
3
- - Use at most 1-2 labels unless there's a compelling reason for more
3
+ - Use at most 1-2 labels unless there's a compelling reason for more. It's ok to use no labels.
4
4
  - Prefer specific labels (bug, feature) over generic ones (question, help wanted)
5
5
  - For PRs that fix bugs, use 'bug' not 'enhancement'
6
6
  - Never combine: bug + enhancement, feature + enhancement. For these labels, only choose the most relevant one.
@@ -12,9 +12,8 @@ labels:
12
12
  instructions: |
13
13
  Apply when describing or fixing unexpected behavior:
14
14
  - Issues: Clear error messages or unexpected outcomes
15
- - PRs: Fixes for broken functionality
16
- Don't apply enhancement/feature for bug fixes unless they add significant new functionality
17
- beyond fixing the bug
15
+ - PRs: Standalone fixes for broken functionality or closing bug reports.
16
+ Don't apply bug unless the issue or PR is predominantly about a specific bug.
18
17
 
19
18
  - documentation:
20
19
  description: "Improvements or additions to documentation"
@@ -28,3 +28,9 @@ jobs:
28
28
  python-version: "3.12"
29
29
  - name: Run pre-commit
30
30
  uses: pre-commit/action@v3.0.1
31
+ - name: Install dependencies
32
+ run: |
33
+ python -m pip install --upgrade pip
34
+ pip install ".[tests]"
35
+ - name: Run pyright
36
+ run: pyright src tests
@@ -16,4 +16,4 @@ src/fastmcp/_version.py
16
16
 
17
17
  # editors
18
18
  .cursorrules
19
- .vscode/
19
+ .vscode/
fastmcp-0.4.1/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Jeremiah Lowin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -1,9 +1,9 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: fastmcp
3
- Version: 0.3.5
3
+ Version: 0.4.1
4
4
  Summary: A more ergonomic interface for MCP servers
5
5
  Author: Jeremiah Lowin
6
- License: Apache-2.0
6
+ License: MIT
7
7
  Requires-Python: >=3.10
8
8
  Requires-Dist: httpx>=0.26.0
9
9
  Requires-Dist: mcp<2.0.0,>=1.0.0
@@ -16,13 +16,17 @@ Requires-Dist: copychat>=0.5.2; extra == 'dev'
16
16
  Requires-Dist: ipython>=8.12.3; extra == 'dev'
17
17
  Requires-Dist: pdbpp>=0.10.3; extra == 'dev'
18
18
  Requires-Dist: pre-commit; extra == 'dev'
19
+ Requires-Dist: pyright>=1.1.389; extra == 'dev'
19
20
  Requires-Dist: pytest-asyncio>=0.23.5; extra == 'dev'
21
+ Requires-Dist: pytest-flakefinder; extra == 'dev'
20
22
  Requires-Dist: pytest-xdist>=3.6.1; extra == 'dev'
21
23
  Requires-Dist: pytest>=8.3.3; extra == 'dev'
22
24
  Requires-Dist: ruff; extra == 'dev'
23
25
  Provides-Extra: tests
24
26
  Requires-Dist: pre-commit; extra == 'tests'
27
+ Requires-Dist: pyright>=1.1.389; extra == 'tests'
25
28
  Requires-Dist: pytest-asyncio>=0.23.5; extra == 'tests'
29
+ Requires-Dist: pytest-flakefinder; extra == 'tests'
26
30
  Requires-Dist: pytest-xdist>=3.6.1; extra == 'tests'
27
31
  Requires-Dist: pytest>=8.3.3; extra == 'tests'
28
32
  Requires-Dist: ruff; extra == 'tests'
@@ -0,0 +1,346 @@
1
+ # /// script
2
+ # dependencies = ["pydantic-ai-slim[openai]", "asyncpg", "numpy", "pgvector", "fastmcp"]
3
+ # ///
4
+
5
+ # uv pip install 'pydantic-ai-slim[openai]' asyncpg numpy pgvector fastmcp
6
+
7
+ """
8
+ Recursive memory system inspired by the human brain's clustering of memories.
9
+ Uses OpenAI's 'text-embedding-3-small' model and pgvector for efficient similarity search.
10
+ """
11
+
12
+ import asyncio
13
+ import math
14
+ import os
15
+ from dataclasses import dataclass
16
+ from datetime import datetime, timezone
17
+ from pathlib import Path
18
+ from typing import Annotated, Self
19
+
20
+ import asyncpg
21
+ import numpy as np
22
+ from openai import AsyncOpenAI
23
+ from pgvector.asyncpg import register_vector # Import register_vector
24
+ from pydantic import BaseModel, Field
25
+ from pydantic_ai import Agent
26
+
27
+ from fastmcp import FastMCP
28
+
29
+ MAX_DEPTH = 5
30
+ SIMILARITY_THRESHOLD = 0.7
31
+ DECAY_FACTOR = 0.99
32
+ REINFORCEMENT_FACTOR = 1.1
33
+
34
+ DEFAULT_LLM_MODEL = "openai:gpt-4o"
35
+ DEFAULT_EMBEDDING_MODEL = "text-embedding-3-small"
36
+
37
+ mcp = FastMCP(
38
+ "memory",
39
+ dependencies=[
40
+ "pydantic-ai-slim[openai]",
41
+ "asyncpg",
42
+ "numpy",
43
+ "pgvector",
44
+ ],
45
+ )
46
+
47
+ DB_DSN = "postgresql://postgres:postgres@localhost:54320/memory_db"
48
+ # reset memory with rm ~/.fastmcp/{USER}/memory/*
49
+ PROFILE_DIR = (
50
+ Path.home() / ".fastmcp" / os.environ.get("USER", "anon") / "memory"
51
+ ).resolve()
52
+ PROFILE_DIR.mkdir(parents=True, exist_ok=True)
53
+
54
+
55
+ def cosine_similarity(a: list[float], b: list[float]) -> float:
56
+ a_array = np.array(a, dtype=np.float64)
57
+ b_array = np.array(b, dtype=np.float64)
58
+ return np.dot(a_array, b_array) / (
59
+ np.linalg.norm(a_array) * np.linalg.norm(b_array)
60
+ )
61
+
62
+
63
+ async def do_ai[T](
64
+ user_prompt: str,
65
+ system_prompt: str,
66
+ result_type: type[T] | Annotated,
67
+ deps=None,
68
+ ) -> T:
69
+ agent = Agent(
70
+ DEFAULT_LLM_MODEL,
71
+ system_prompt=system_prompt,
72
+ result_type=result_type,
73
+ )
74
+ result = await agent.run(user_prompt, deps=deps)
75
+ return result.data
76
+
77
+
78
+ @dataclass
79
+ class Deps:
80
+ openai: AsyncOpenAI
81
+ pool: asyncpg.Pool
82
+
83
+
84
+ async def get_db_pool() -> asyncpg.Pool:
85
+ async def init(conn):
86
+ await conn.execute("CREATE EXTENSION IF NOT EXISTS vector;")
87
+ await register_vector(conn)
88
+
89
+ pool = await asyncpg.create_pool(DB_DSN, init=init)
90
+ return pool
91
+
92
+
93
+ class MemoryNode(BaseModel):
94
+ id: int | None = None
95
+ content: str
96
+ summary: str = ""
97
+ importance: float = 1.0
98
+ access_count: int = 0
99
+ timestamp: float = Field(
100
+ default_factory=lambda: datetime.now(timezone.utc).timestamp()
101
+ )
102
+ embedding: list[float]
103
+
104
+ @classmethod
105
+ async def from_content(cls, content: str, deps: Deps):
106
+ embedding = await get_embedding(content, deps)
107
+ return cls(content=content, embedding=embedding)
108
+
109
+ async def save(self, deps: Deps):
110
+ async with deps.pool.acquire() as conn:
111
+ if self.id is None:
112
+ result = await conn.fetchrow(
113
+ """
114
+ INSERT INTO memories (content, summary, importance, access_count, timestamp, embedding)
115
+ VALUES ($1, $2, $3, $4, $5, $6)
116
+ RETURNING id
117
+ """,
118
+ self.content,
119
+ self.summary,
120
+ self.importance,
121
+ self.access_count,
122
+ self.timestamp,
123
+ self.embedding,
124
+ )
125
+ self.id = result["id"]
126
+ else:
127
+ await conn.execute(
128
+ """
129
+ UPDATE memories
130
+ SET content = $1, summary = $2, importance = $3,
131
+ access_count = $4, timestamp = $5, embedding = $6
132
+ WHERE id = $7
133
+ """,
134
+ self.content,
135
+ self.summary,
136
+ self.importance,
137
+ self.access_count,
138
+ self.timestamp,
139
+ self.embedding,
140
+ self.id,
141
+ )
142
+
143
+ async def merge_with(self, other: Self, deps: Deps):
144
+ self.content = await do_ai(
145
+ f"{self.content}\n\n{other.content}",
146
+ "Combine the following two texts into a single, coherent text.",
147
+ str,
148
+ deps,
149
+ )
150
+ self.importance += other.importance
151
+ self.access_count += other.access_count
152
+ self.embedding = [(a + b) / 2 for a, b in zip(self.embedding, other.embedding)]
153
+ self.summary = await do_ai(
154
+ self.content, "Summarize the following text concisely.", str, deps
155
+ )
156
+ await self.save(deps)
157
+ # Delete the merged node from the database
158
+ if other.id is not None:
159
+ await delete_memory(other.id, deps)
160
+
161
+ def get_effective_importance(self):
162
+ return self.importance * (1 + math.log(self.access_count + 1))
163
+
164
+
165
+ async def get_embedding(text: str, deps: Deps) -> list[float]:
166
+ embedding_response = await deps.openai.embeddings.create(
167
+ input=text,
168
+ model=DEFAULT_EMBEDDING_MODEL,
169
+ )
170
+ return embedding_response.data[0].embedding
171
+
172
+
173
+ async def delete_memory(memory_id: int, deps: Deps):
174
+ async with deps.pool.acquire() as conn:
175
+ await conn.execute("DELETE FROM memories WHERE id = $1", memory_id)
176
+
177
+
178
+ async def add_memory(content: str, deps: Deps):
179
+ new_memory = await MemoryNode.from_content(content, deps)
180
+ await new_memory.save(deps)
181
+
182
+ similar_memories = await find_similar_memories(new_memory.embedding, deps)
183
+ for memory in similar_memories:
184
+ if memory.id != new_memory.id:
185
+ await new_memory.merge_with(memory, deps)
186
+
187
+ await update_importance(new_memory.embedding, deps)
188
+
189
+ await prune_memories(deps)
190
+
191
+ return f"Remembered: {content}"
192
+
193
+
194
+ async def find_similar_memories(embedding: list[float], deps: Deps) -> list[MemoryNode]:
195
+ async with deps.pool.acquire() as conn:
196
+ rows = await conn.fetch(
197
+ """
198
+ SELECT id, content, summary, importance, access_count, timestamp, embedding
199
+ FROM memories
200
+ ORDER BY embedding <-> $1
201
+ LIMIT 5
202
+ """,
203
+ embedding,
204
+ )
205
+ memories = [
206
+ MemoryNode(
207
+ id=row["id"],
208
+ content=row["content"],
209
+ summary=row["summary"],
210
+ importance=row["importance"],
211
+ access_count=row["access_count"],
212
+ timestamp=row["timestamp"],
213
+ embedding=row["embedding"],
214
+ )
215
+ for row in rows
216
+ ]
217
+ return memories
218
+
219
+
220
+ async def update_importance(user_embedding: list[float], deps: Deps):
221
+ async with deps.pool.acquire() as conn:
222
+ rows = await conn.fetch(
223
+ "SELECT id, importance, access_count, embedding FROM memories"
224
+ )
225
+ for row in rows:
226
+ memory_embedding = row["embedding"]
227
+ similarity = cosine_similarity(user_embedding, memory_embedding)
228
+ if similarity > SIMILARITY_THRESHOLD:
229
+ new_importance = row["importance"] * REINFORCEMENT_FACTOR
230
+ new_access_count = row["access_count"] + 1
231
+ else:
232
+ new_importance = row["importance"] * DECAY_FACTOR
233
+ new_access_count = row["access_count"]
234
+ await conn.execute(
235
+ """
236
+ UPDATE memories
237
+ SET importance = $1, access_count = $2
238
+ WHERE id = $3
239
+ """,
240
+ new_importance,
241
+ new_access_count,
242
+ row["id"],
243
+ )
244
+
245
+
246
+ async def prune_memories(deps: Deps):
247
+ async with deps.pool.acquire() as conn:
248
+ rows = await conn.fetch(
249
+ """
250
+ SELECT id, importance, access_count
251
+ FROM memories
252
+ ORDER BY importance DESC
253
+ OFFSET $1
254
+ """,
255
+ MAX_DEPTH,
256
+ )
257
+ for row in rows:
258
+ await conn.execute("DELETE FROM memories WHERE id = $1", row["id"])
259
+
260
+
261
+ async def display_memory_tree(deps: Deps) -> str:
262
+ async with deps.pool.acquire() as conn:
263
+ rows = await conn.fetch(
264
+ """
265
+ SELECT content, summary, importance, access_count
266
+ FROM memories
267
+ ORDER BY importance DESC
268
+ LIMIT $1
269
+ """,
270
+ MAX_DEPTH,
271
+ )
272
+ result = ""
273
+ for row in rows:
274
+ effective_importance = row["importance"] * (
275
+ 1 + math.log(row["access_count"] + 1)
276
+ )
277
+ summary = row["summary"] or row["content"]
278
+ result += f"- {summary} (Importance: {effective_importance:.2f})\n"
279
+ return result
280
+
281
+
282
+ @mcp.tool()
283
+ async def remember(
284
+ contents: list[str] = Field(
285
+ description="List of observations or memories to store"
286
+ ),
287
+ ):
288
+ deps = Deps(openai=AsyncOpenAI(), pool=await get_db_pool())
289
+ try:
290
+ return "\n".join(
291
+ await asyncio.gather(*[add_memory(content, deps) for content in contents])
292
+ )
293
+ finally:
294
+ await deps.pool.close()
295
+
296
+
297
+ @mcp.tool()
298
+ async def read_profile() -> str:
299
+ deps = Deps(openai=AsyncOpenAI(), pool=await get_db_pool())
300
+ profile = await display_memory_tree(deps)
301
+ await deps.pool.close()
302
+ return profile
303
+
304
+
305
+ async def initialize_database():
306
+ pool = await asyncpg.create_pool(
307
+ "postgresql://postgres:postgres@localhost:54320/postgres"
308
+ )
309
+ try:
310
+ async with pool.acquire() as conn:
311
+ await conn.execute("""
312
+ SELECT pg_terminate_backend(pg_stat_activity.pid)
313
+ FROM pg_stat_activity
314
+ WHERE pg_stat_activity.datname = 'memory_db'
315
+ AND pid <> pg_backend_pid();
316
+ """)
317
+ await conn.execute("DROP DATABASE IF EXISTS memory_db;")
318
+ await conn.execute("CREATE DATABASE memory_db;")
319
+ finally:
320
+ await pool.close()
321
+
322
+ pool = await asyncpg.create_pool(DB_DSN)
323
+ try:
324
+ async with pool.acquire() as conn:
325
+ await conn.execute("CREATE EXTENSION IF NOT EXISTS vector;")
326
+
327
+ await register_vector(conn)
328
+
329
+ await conn.execute("""
330
+ CREATE TABLE IF NOT EXISTS memories (
331
+ id SERIAL PRIMARY KEY,
332
+ content TEXT NOT NULL,
333
+ summary TEXT,
334
+ importance REAL NOT NULL,
335
+ access_count INT NOT NULL,
336
+ timestamp DOUBLE PRECISION NOT NULL,
337
+ embedding vector(1536) NOT NULL
338
+ );
339
+ CREATE INDEX IF NOT EXISTS idx_memories_embedding ON memories USING hnsw (embedding vector_l2_ops);
340
+ """)
341
+ finally:
342
+ await pool.close()
343
+
344
+
345
+ if __name__ == "__main__":
346
+ asyncio.run(initialize_database())
@@ -13,7 +13,7 @@ dependencies = [
13
13
  ]
14
14
  requires-python = ">=3.10"
15
15
  readme = "README.md"
16
- license = { text = "Apache-2.0" }
16
+ license = { text = "MIT" }
17
17
 
18
18
  [project.scripts]
19
19
  fastmcp = "fastmcp.cli:app"
@@ -25,8 +25,10 @@ build-backend = "hatchling.build"
25
25
  [project.optional-dependencies]
26
26
  tests = [
27
27
  "pre-commit",
28
+ "pyright>=1.1.389",
28
29
  "pytest>=8.3.3",
29
30
  "pytest-asyncio>=0.23.5",
31
+ "pytest-flakefinder",
30
32
  "pytest-xdist>=3.6.1",
31
33
  "ruff",
32
34
  ]
@@ -38,3 +40,15 @@ asyncio_default_fixture_loop_scope = "session"
38
40
 
39
41
  [tool.hatch.version]
40
42
  source = "vcs"
43
+
44
+ [tool.pyright]
45
+ include = ["src", "tests"]
46
+ exclude = ["**/node_modules", "**/__pycache__", ".venv", ".git", "dist"]
47
+ pythonVersion = "3.10"
48
+ pythonPlatform = "Darwin"
49
+ typeCheckingMode = "basic"
50
+ reportMissingImports = true
51
+ reportMissingTypeStubs = false
52
+ useLibraryCodeForTypes = true
53
+ venvPath = "."
54
+ venv = ".venv"
@@ -2,14 +2,15 @@
2
2
 
3
3
  import importlib.metadata
4
4
  import importlib.util
5
+ import os
5
6
  import subprocess
6
7
  import sys
7
8
  from pathlib import Path
8
- from typing import Optional, Tuple, Dict
9
+ from typing import Dict, Optional, Tuple
9
10
 
11
+ import dotenv
10
12
  import typer
11
13
  from typing_extensions import Annotated
12
- import dotenv
13
14
 
14
15
  from fastmcp.cli import claude
15
16
  from fastmcp.utilities.logging import get_logger
@@ -242,6 +243,7 @@ def dev(
242
243
  [npx_cmd, "@modelcontextprotocol/inspector"] + uv_cmd,
243
244
  check=True,
244
245
  shell=shell,
246
+ env=dict(os.environ.items()), # Convert to list of tuples for env update
245
247
  )
246
248
  sys.exit(process.returncode)
247
249
  except subprocess.CalledProcessError as e:
@@ -423,7 +425,11 @@ def install(
423
425
  # Load from .env file if specified
424
426
  if env_file:
425
427
  try:
426
- env_dict.update(dotenv.dotenv_values(env_file))
428
+ env_dict |= {
429
+ k: v
430
+ for k, v in dotenv.dotenv_values(env_file).items()
431
+ if v is not None
432
+ }
427
433
  except Exception as e:
428
434
  logger.error(f"Failed to load .env file: {e}")
429
435
  sys.exit(1)
@@ -1,43 +1,52 @@
1
1
  """Base classes for FastMCP prompts."""
2
2
 
3
3
  import json
4
- from typing import Any, Callable, Dict, Literal, Optional, Sequence, Union
4
+ from typing import Any, Callable, Dict, Literal, Optional, Sequence, Awaitable
5
5
  import inspect
6
6
 
7
- from pydantic import BaseModel, Field, TypeAdapter, field_validator, validate_call
7
+ from pydantic import BaseModel, Field, TypeAdapter, validate_call
8
8
  from mcp.types import TextContent, ImageContent, EmbeddedResource
9
9
  import pydantic_core
10
10
 
11
+ CONTENT_TYPES = TextContent | ImageContent | EmbeddedResource
12
+
11
13
 
12
14
  class Message(BaseModel):
13
15
  """Base class for all prompt messages."""
14
16
 
15
17
  role: Literal["user", "assistant"]
16
- content: Union[TextContent, ImageContent, EmbeddedResource]
18
+ content: CONTENT_TYPES
17
19
 
18
- def __init__(self, content, **kwargs):
20
+ def __init__(self, content: str | CONTENT_TYPES, **kwargs):
21
+ if isinstance(content, str):
22
+ content = TextContent(type="text", text=content)
19
23
  super().__init__(content=content, **kwargs)
20
24
 
21
- @field_validator("content", mode="before")
22
- def validate_content(cls, v):
23
- if isinstance(v, str):
24
- return TextContent(type="text", text=v)
25
- return v
26
-
27
25
 
28
26
  class UserMessage(Message):
29
27
  """A message from the user."""
30
28
 
31
29
  role: Literal["user"] = "user"
32
30
 
31
+ def __init__(self, content: str | CONTENT_TYPES, **kwargs):
32
+ super().__init__(content=content, **kwargs)
33
+
33
34
 
34
35
  class AssistantMessage(Message):
35
36
  """A message from the assistant."""
36
37
 
37
38
  role: Literal["assistant"] = "assistant"
38
39
 
40
+ def __init__(self, content: str | CONTENT_TYPES, **kwargs):
41
+ super().__init__(content=content, **kwargs)
42
+
43
+
44
+ message_validator = TypeAdapter(UserMessage | AssistantMessage)
39
45
 
40
- message_validator = TypeAdapter(Union[UserMessage, AssistantMessage])
46
+ SyncPromptResult = (
47
+ str | Message | dict[str, Any] | Sequence[str | Message | dict[str, Any]]
48
+ )
49
+ PromptResult = SyncPromptResult | Awaitable[SyncPromptResult]
41
50
 
42
51
 
43
52
  class PromptArgument(BaseModel):
@@ -67,11 +76,18 @@ class Prompt(BaseModel):
67
76
  @classmethod
68
77
  def from_function(
69
78
  cls,
70
- fn: Callable[..., Sequence[Message]],
79
+ fn: Callable[..., PromptResult],
71
80
  name: Optional[str] = None,
72
81
  description: Optional[str] = None,
73
82
  ) -> "Prompt":
74
- """Create a Prompt from a function."""
83
+ """Create a Prompt from a function.
84
+
85
+ The function can return:
86
+ - A string (converted to a message)
87
+ - A Message object
88
+ - A dict (converted to a message)
89
+ - A sequence of any of the above
90
+ """
75
91
  func_name = name or fn.__name__
76
92
 
77
93
  if func_name == "<lambda>":
@@ -1,14 +1,14 @@
1
1
  """Base classes and interfaces for FastMCP resources."""
2
2
 
3
3
  import abc
4
- from typing import Union
4
+ from typing import Union, Annotated
5
5
 
6
6
  from pydantic import (
7
7
  AnyUrl,
8
8
  BaseModel,
9
9
  ConfigDict,
10
10
  Field,
11
- FileUrl,
11
+ UrlConstraints,
12
12
  ValidationInfo,
13
13
  field_validator,
14
14
  )
@@ -19,8 +19,9 @@ class Resource(BaseModel, abc.ABC):
19
19
 
20
20
  model_config = ConfigDict(validate_default=True)
21
21
 
22
- # uri: Annotated[AnyUrl, BeforeValidator(maybe_cast_str_to_any_url)] = Field(
23
- uri: AnyUrl = Field(default=..., description="URI of the resource")
22
+ uri: Annotated[AnyUrl, UrlConstraints(host_required=False)] = Field(
23
+ default=..., description="URI of the resource"
24
+ )
24
25
  name: str | None = Field(description="Name of the resource", default=None)
25
26
  description: str | None = Field(
26
27
  description="Description of the resource", default=None
@@ -31,15 +32,6 @@ class Resource(BaseModel, abc.ABC):
31
32
  pattern=r"^[a-zA-Z0-9]+/[a-zA-Z0-9\-+.]+$",
32
33
  )
33
34
 
34
- @field_validator("uri", mode="before")
35
- def validate_uri(cls, uri: AnyUrl | str) -> AnyUrl:
36
- if isinstance(uri, str):
37
- # AnyUrl doesn't support triple-slashes, but files do ("file:///absolute/path")
38
- if uri.startswith("file://"):
39
- return FileUrl(uri)
40
- return AnyUrl(uri)
41
- return uri
42
-
43
35
  @field_validator("name", mode="before")
44
36
  @classmethod
45
37
  def set_default_name(cls, name: str | None, info: ValidationInfo) -> str:
@@ -70,7 +70,7 @@ class ResourceTemplate(BaseModel):
70
70
  result = await result
71
71
 
72
72
  return FunctionResource(
73
- uri=uri,
73
+ uri=uri, # type: ignore
74
74
  name=self.name,
75
75
  description=self.description,
76
76
  mime_type=self.mime_type,
@@ -8,7 +8,7 @@ from typing import Any, Callable, Union
8
8
  import httpx
9
9
  import pydantic.json
10
10
  import pydantic_core
11
- from pydantic import Field
11
+ from pydantic import Field, ValidationInfo
12
12
 
13
13
  from fastmcp.resources.base import Resource
14
14
 
@@ -91,6 +91,15 @@ class FileResource(Resource):
91
91
  raise ValueError("Path must be absolute")
92
92
  return path
93
93
 
94
+ @pydantic.field_validator("is_binary")
95
+ @classmethod
96
+ def set_binary_from_mime_type(cls, is_binary: bool, info: ValidationInfo) -> bool:
97
+ """Set is_binary based on mime_type if not explicitly set."""
98
+ if is_binary:
99
+ return True
100
+ mime_type = info.data.get("mime_type", "text/plain")
101
+ return not mime_type.startswith("text/")
102
+
94
103
  async def read(self) -> Union[str, bytes]:
95
104
  """Read the file content."""
96
105
  try: