1
- """
2
- Simple example of using FastAPI-MCP to add an MCP server to a FastAPI app.
3
- """
4
-
5
- from fastapi import FastAPI , HTTPException , Query
6
- from pydantic import BaseModel
7
- from typing import List , Optional
8
-
9
-
10
- router = FastAPI ()
11
-
12
-
13
- class Item (BaseModel ):
14
- id : int
15
- name : str
16
- description : Optional [str ] = None
17
- price : float
18
- tags : List [str ] = []
19
-
20
-
21
- items_db : dict [int , Item ] = {}
22
-
23
-
24
- @router .get ("/items/" , response_model = List [Item ], tags = ["items" ], operation_id = "list_items" )
25
- async def list_items (skip : int = 0 , limit : int = 10 ):
26
- """
27
- List all items in the database.
28
-
29
- Returns a list of items, with pagination support.
30
- """
31
- return list (items_db .values ())[skip : skip + limit ]
32
-
33
-
34
- @router .get ("/items/{item_id}" , response_model = Item , tags = ["items" ], operation_id = "get_item" )
35
- async def read_item (item_id : int ):
36
- """
37
- Get a specific item by its ID.
38
-
39
- Raises a 404 error if the item does not exist.
40
- """
41
- if item_id not in items_db :
42
- raise HTTPException (status_code = 404 , detail = "Item not found" )
43
- return items_db [item_id ]
44
-
45
-
46
- @router .post ("/items/" , response_model = Item , tags = ["items" ], operation_id = "create_item" )
47
- async def create_item (item : Item ):
48
- """
49
- Create a new item in the database.
50
-
51
- Returns the created item with its assigned ID.
52
- """
53
- items_db [item .id ] = item
54
- return item
55
-
56
-
57
- @router .put ("/items/{item_id}" , response_model = Item , tags = ["items" ], operation_id = "update_item" )
58
- async def update_item (item_id : int , item : Item ):
59
- """
60
- Update an existing item.
61
-
62
- Raises a 404 error if the item does not exist.
63
- """
64
- if item_id not in items_db :
65
- raise HTTPException (status_code = 404 , detail = "Item not found" )
66
-
67
- item .id = item_id
68
- items_db [item_id ] = item
69
- return item
70
-
71
-
72
- @router .delete ("/items/{item_id}" , tags = ["items" ], operation_id = "delete_item" )
73
- async def delete_item (item_id : int ):
74
- """
75
- Delete an item from the database.
76
-
77
- Raises a 404 error if the item does not exist.
78
- """
79
- if item_id not in items_db :
80
- raise HTTPException (status_code = 404 , detail = "Item not found" )
81
-
82
- del items_db [item_id ]
83
- return {"message" : "Item deleted successfully" }
84
-
85
-
86
- @router .get ("/items/search/" , response_model = List [Item ], tags = ["search" ], operation_id = "search_items" )
87
- async def search_items (
88
- q : Optional [str ] = Query (None , description = "Search query string" ),
89
- min_price : Optional [float ] = Query (None , description = "Minimum price" ),
90
- max_price : Optional [float ] = Query (None , description = "Maximum price" ),
91
- tags : List [str ] = Query ([], description = "Filter by tags" ),
92
- ):
93
- """
94
- Search for items with various filters.
95
-
96
- Returns a list of items that match the search criteria.
97
- """
98
- results = list (items_db .values ())
99
-
100
- if q :
101
- q = q .lower ()
102
- results = [
103
- item for item in results if q in item .name .lower () or (item .description and q in item .description .lower ())
104
- ]
105
-
106
- if min_price is not None :
107
- results = [item for item in results if item .price >= min_price ]
108
- if max_price is not None :
109
- results = [item for item in results if item .price <= max_price ]
110
-
111
- if tags :
112
- results = [item for item in results if all (tag in item .tags for tag in tags )]
113
-
114
- return results
115
-
116
-
117
- sample_items = [
118
- Item (id = 1 , name = "Hammer" , description = "A tool for hammering nails" , price = 9.99 , tags = ["tool" , "hardware" ]),
119
- Item (id = 2 , name = "Screwdriver" , description = "A tool for driving screws" , price = 7.99 , tags = ["tool" , "hardware" ]),
120
- Item (id = 3 , name = "Wrench" , description = "A tool for tightening bolts" , price = 12.99 , tags = ["tool" , "hardware" ]),
121
- Item (id = 4 , name = "Saw" , description = "A tool for cutting wood" , price = 19.99 , tags = ["tool" , "hardware" , "cutting" ]),
122
- Item (id = 5 , name = "Drill" , description = "A tool for drilling holes" , price = 49.99 , tags = ["tool" , "hardware" , "power" ]),
123
- ]
124
- for item in sample_items :
125
- items_db [item .id ] = item
0 commit comments