昨日在測試時發現使用天氣及地震時輸入要求會出現錯誤,結論是要parameters,所以又小改了一下
.yaml
openapi: 3.0.3
info:
title: Weather & Earthquake MCP Tools
version: 1.0.0
description: 天氣和地震查詢工具
servers:
- url: http://localhost:8001
description: REST API 伺服器
paths:
/weather:
post:
operationId: getWeather
summary: 查詢天氣
description: 根據縣市名稱查詢天氣資訊
parameters:
- name: LocationName
in: query
required: false
description: 臺灣各縣市名稱
schema:
type: string
- name: ElementName
in: query
required: false
description: 天氣預報因子
schema:
type: string
responses:
'200':
description: 天氣結果
content:
application/json:
schema:
type: object
properties:
city:
type: string
description: 城市名稱
temperature:
type: string
description: 溫度資訊
description:
type: string
description: 天氣描述
/earthquake:
post:
operationId: getEarthquakes
summary: 取得地震資料
description: 可指定縣市或取得全部最新地震
parameters:
- name: target_areas
in: query
required: false
description: 目標縣市名稱
schema:
type: string
- name: limit
in: query
required: false
description: 限制回傳地震筆數
schema:
type: string
responses:
'200':
description: 地震查詢結果
content:
application/json:
schema:
type: object
properties:
success:
type: boolean
description: 查詢是否成功
message:
type: string
description: 回應訊息
data:
type: array
description: 地震資料列表
items:
type: object
properties:
time:
type: string
description: 地震時間
location:
type: string
description: 地震位置
magnitude:
type: string
description: 地震規模
depth:
type: string
description: 地震深度
rest_api
@app.post("/earthquake")
async def post_earthquakes(
# Query 參數 (來自 MCP)
target_areas: Optional[str] = Query(None, description="目標縣市名稱"),
limit: Optional[str] = Query("5", description="限制回傳地震筆數"),
# Request Body (來自傳統 HTTP 客戶端)
request: Optional[EarthquakeRequest] = Body(None)
):
# 優先使用 query 參數,如果沒有則使用 request body
final_target_areas = target_areas or (request.target_areas if request else None)
final_limit = limit if limit != "5" else (request.limit if request else "5")
接著是今天的目標,要接收discord的訊息然後回傳我要的地震或天氣功能
首先到設定,選擇community nodes 右上角install輸入@jordanburke/n8n-nodes-discord
安裝好後就可以在自己的n8n中找到discord trigger了
接著需要Credential
但要使用的話,由於之前的權限不同,要再創一個機器人
https://discord.com/developers/applications
到OAuth2,最上面的client ID
接著下面勾選bot、applications.command、Adminstrator
接著到bot勾選這些
然後加到你的discord群組
bot token之前說過這邊不介紹
回到n8n,到Setting,選擇n8n api,新增api,如可以更改過期時間,我選擇無限制
這樣Credential要的資料就全部都有了
接著重開n8n就可以看到它可以正常讀取discord的頻道了,明天再正式使用他