mcp前端
MCP前端应用程序
mcp-frontend copilot prompt
提示 我需要用Python创建一个web应用程序,作为AI应用程序的前端。与AI的互动应用程序将通过REST API,请求将通过中的POST发送到 message 参数。应用程序必须使用橙色配色方案,具有响应性和视觉吸引力。它应该具有基于角色的身份验证和授权(管理员、用户、查看器),最初基于Google Cloud Platform(GCP)BigQuery中的表。应用程序必须准备好使用虚拟机和Terraform脚本在GCP上部署。Terraform脚本还将创建必要的资源,例如名为的服务帐户 mcp-sre-assistant-sa需要一组测试用例。最初,模拟AI在创建实际的REST API时,还需要应用程序的REST API来进行开发。类名、变量名和代码文档必须使用英语。
副驾驶提示:https://copilot.microsoft.com/chats/i8scNkMN7iyAGUAEeJmE2
mcp-frontend app dir structure
mcp-sre-assistant/
├── README.md
├── app
│ ├── __init__.py
│ ├── ai_client.py
│ ├── auth.py
│ ├── bigquery_client.py
│ ├── main.py
│ ├── rbac.py
│ ├── settings.py
│ ├── static
│ └── templates
├── mock_ai
│ └── server.py
├── pyproject.toml
├── requirements.txt
├── sql
│ └── users.sql
├── terraform
│ ├── README.md
│ ├── main.tf
│ ├── outputs.tf
│ ├── startup.sh
│ ├── startup.sh.bkp
│ └── variables.tf
├── tests
│ ├── test_ai_integration.py
│ ├── test_auth.py
│ └── test_rbac.py
└── venv3.11跑
Run App
# python 3.11
/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11 -m venv venv3.11
source venv3.11/bin/activate
# Create venv
which pip
./venv3.11/bin/python3 -m pip install uvicorn fastapi jinja2 google-cloud-bigquery httpx python-dotenv starlette itsdangerous pydantic_settings python-multipart
./venv3.11/bin/python3 -m pip install "passlib[argon2]"
# Create app as a package
# Add packages = ["app"] in pyproject.toml
pip install -e .
# Run
which python
./venv3.11/bin/python3 -m uvicorn app.main:app --host 0.0.0.0 --port 8000Run tests
# python 3.11
/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11 -m venv venv3.11
source venv3.11/bin/activate
# Create venv
which python
pip install ipympl ipython jupyterlab matplotlib nodejs numpy seaborn starlette fastapi pytest itsdangerous pydantic_settings passlib google-cloud-bigquery python-multipart
#uvicorn jinja2 passlib[argon2] httpx python-dotenv
# Create app as a package
which pip
# Add packages = ["app"] in pyproject.toml
pip install -e .
# Run
which pytest
pytest -q用户指南
应用程序的使用非常简单,只需登录应用程序,并向人工智能请求一些东西。人工智能会做出回应
步骤,
- 启动应用程序(如果需要):
run-app.sh - 启动模拟服务器(如果需要):
run-mock.sh - 使用URL登录http://0.0.0.0:8000/login
- 用户:jarmando_ml@hotmail.com, armando.martinez.esausi@gmail.com
- 请求
- 结果
Screen capture examples
登录 Login
请求 Request
结果 Result
Terminal logs when normal behavior
[notice] To update, run: pip install --upgrade pip
INFO: Started server process [39937]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO: 127.0.0.1:53713 - "POST /login HTTP/1.1" 303 See Other
INFO: 127.0.0.1:53713 - "GET /dashboard HTTP/1.1" 200 OK
INFO: 127.0.0.1:54047 - "POST /chat HTTP/1.1" 200 OK
INFO: 127.0.0.1:54065 - "GET /dashboard HTTP/1.1" 200 OK
INFO: 127.0.0.1:54099 - "POST /chat HTTP/1.1" 200 OK
INFO: 127.0.0.1:54271 - "GET /logout HTTP/1.1" 303 See Other
INFO: 127.0.0.1:54271 - "GET /login HTTP/1.1" 200 OK
INFO: 127.0.0.1:54291 - "POST /login HTTP/1.1" 401 Unauthorized
INFO: 127.0.0.1:54327 - "POST /login HTTP/1.1" 303 See Other
INFO: 127.0.0.1:54327 - "GET /dashboard HTTP/1.1" 200 OKERROR: Mock server is down
INFO: 127.0.0.1:53757 - "POST /chat HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/httpx/_transports/default.py", line 101, in map_httpcore_exceptions
yield
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/httpx/_transports/default.py", line 394, in handle_async_request
resp = await self._pool.handle_async_request(req)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/httpcore/_async/connection_pool.py", line 256, in handle_async_request
raise exc from None
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/httpcore/_async/connection_pool.py", line 236, in handle_async_request
response = await connection.handle_async_request(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/httpcore/_async/connection.py", line 101, in handle_async_request
raise exc
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/httpcore/_async/connection.py", line 78, in handle_async_request
stream = await self._connect(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/httpcore/_async/connection.py", line 124, in _connect
stream = await self._network_backend.connect_tcp(**kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/httpcore/_backends/auto.py", line 31, in connect_tcp
return await self._backend.connect_tcp(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/httpcore/_backends/anyio.py", line 113, in connect_tcp
with map_exceptions(exc_map):
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/contextlib.py", line 155, in __exit__
self.gen.throw(typ, value, traceback)
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/httpcore/_exceptions.py", line 14, in map_exceptions
raise to_exc(exc) from exc
httpcore.ConnectError: All connection attempts failed
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
result = await app( # type: ignore[func-returns-value]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
return await self.app(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/fastapi/applications.py", line 1135, in __call__
await super().__call__(scope, receive, send)
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/starlette/applications.py", line 107, in __call__
await self.middleware_stack(scope, receive, send)
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/starlette/middleware/errors.py", line 186, in __call__
raise exc
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/starlette/middleware/errors.py", line 164, in __call__
await self.app(scope, receive, _send)
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/starlette/middleware/sessions.py", line 85, in __call__
await self.app(scope, receive, send_wrapper)
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 63, in __call__
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
raise exc
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
await app(scope, receive, sender)
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
await self.app(scope, receive, send)
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/starlette/routing.py", line 716, in __call__
await self.middleware_stack(scope, receive, send)
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/starlette/routing.py", line 736, in app
await route.handle(scope, receive, send)
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/starlette/routing.py", line 290, in handle
await self.app(scope, receive, send)
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/fastapi/routing.py", line 117, in app
await wrap_app_handling_exceptions(app, request)(scope, receive, send)
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
raise exc
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
await app(scope, receive, sender)
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/fastapi/routing.py", line 103, in app
response = await f(request)
^^^^^^^^^^^^^^^^
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/fastapi/routing.py", line 424, in app
raw_response = await run_endpoint_function(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/fastapi/routing.py", line 310, in run_endpoint_function
return await dependant.call(**values)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/armando/prg/git-hub/ai/mcp-frontend/app/main.py", line 32, in chat
ai_resp = await send_message_to_ai(message)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/armando/prg/git-hub/ai/mcp-frontend/app/ai_client.py", line 7, in send_message_to_ai
r = await client.post(settings.ai_api_url, json={"message": message})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/httpx/_client.py", line 1859, in post
return await self.request(
^^^^^^^^^^^^^^^^^^^
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/httpx/_client.py", line 1540, in request
return await self.send(request, auth=auth, follow_redirects=follow_redirects)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/httpx/_client.py", line 1629, in send
response = await self._send_handling_auth(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/httpx/_client.py", line 1657, in _send_handling_auth
response = await self._send_handling_redirects(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/httpx/_client.py", line 1694, in _send_handling_redirects
response = await self._send_single_request(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/httpx/_client.py", line 1730, in _send_single_request
response = await transport.handle_async_request(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/httpx/_transports/default.py", line 393, in handle_async_request
with map_httpcore_exceptions():
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/contextlib.py", line 155, in __exit__
self.gen.throw(typ, value, traceback)
File "/Users/armando/prg/git-hub/ai/mcp-frontend/venv3.11/lib/python3.11/site-packages/httpx/_transports/default.py", line 118, in map_httpcore_exceptions
raise mapped_exc(message) from exc
httpx.ConnectError: All connection attempts failed如何修复它:
- 使用shell脚本“run-mock.sh”启动模拟服务器
