完整內容在此, 幹話王_續談 MCP Server︰Resource、Server Inspector
繼上一篇 了解 MCP Go 只談到 Tools,以及 MCP client 與 server 之間的交互過程。這次我們來看看另外兩個功能(Capability) Resource 與 Prompt。
Resource 在 MCP Server中,用來表示 Client 能夠存取該 Server 哪些內部的資源或者提供訂製化的請求,並且把這些內容當作跟 LLM 交互的上下文來使用,使得 AI 給我們更加精準的回應。所以不同的 MCP Client 可能有不同的方式決定何時(When)以及如何(How)使用這些 Resource。
官方舉例這些都是 Resource︰
File contents
Database records
API responses
Live system data
Screenshots and images
Log files
And more
Each resource is identified by a unique URI and can contain either text or binary data.
但如果只是要索取資料跟內容,感覺上 Tools 也是能作到,也是沒錯的!只是 Tools 能做到去修改狀態(額外給予 LLM R/W 權限)。而 Resource 不行(額外給予 LLM ReadOnly 權限),且 Resource 語意上主要是提供結構化資料以及所在的位子(URI)提供存取。
所以在 Tools 的說明有這段。
Like resources, tools are identified by unique names and can include descriptions to guide their usage. However, unlike resources, tools represent dynamic operations that can modify state or interact with external systems.
所以你可以理解成 Resoruce 能回傳有哪些 databases 以及 tables。而Tools則是允許你去操作(CRUD+計算)。
讓我們來看看 MCP Postgres Server 的這段程式碼。它就把所有 public 下的 table 給組成 resources,並且每張 table 都有給上 URI,例如 postgres://database/customers/schema
。
server.setRequestHandler(ListResourcesRequestSchema, async () => {
const client = await pool.connect();
try {
const result = await client.query(
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'",
);
return {
resources: result.rows.map((row) => ({
uri: new URL(`${row.table_name}/${SCHEMA_PATH}`, resourceBaseUrl).href,
mimeType: "application/json",
name: `"${row.table_name}" database schema`,
})),
};
} finally {
client.release();
}
});
完整內容在此, 幹話王_續談 MCP Server︰Resource、Server Inspector