Skip to main content
Glama

Hono MCP Sample Server

by y-moriya
index.ts3.33 kB
import { Hono } from 'hono' import { serve } from '@hono/node-server' import { McpServer, ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js' import { StreamableHTTPTransport } from '@hono/mcp' import { z } from 'zod' const app = new Hono() // MCPサーバーの作成 const mcpServer = new McpServer({ name: 'sample-mcp-server', version: '1.0.0', }) // リソースを登録 mcpServer.registerResource( 'weather', new ResourceTemplate('weather://tokyo', { list: undefined }), { description: '東京の現在の天気情報', mimeType: 'text/plain', }, async (uri) => ({ contents: [ { uri: uri.href, mimeType: 'text/plain', text: '東京は晴れ、気温25度です', }, ], }) ) mcpServer.registerResource( 'news', new ResourceTemplate('news://latest', { list: undefined }), { description: '最新のニュースヘッドライン', mimeType: 'application/json', }, async (uri) => ({ contents: [ { uri: uri.href, mimeType: 'application/json', text: JSON.stringify({ headlines: [ { title: 'ニュース1', date: '2025-11-23' }, { title: 'ニュース2', date: '2025-11-23' }, ], }), }, ], }) ) // ツールを登録 mcpServer.registerTool( 'calculator', { description: '2つの数値を加算します', inputSchema: { a: z.number().describe('最初の数値'), b: z.number().describe('2番目の数値'), }, }, async ({ a, b }) => { const result = a + b return { content: [ { type: 'text', text: `計算結果: ${a} + ${b} = ${result}`, }, ], } } ) mcpServer.registerTool( 'reverse-string', { description: '文字列を反転します', inputSchema: { text: z.string().describe('反転する文字列'), }, }, async ({ text }) => { const reversed = text.split('').reverse().join('') return { content: [ { type: 'text', text: `反転結果: ${reversed}`, }, ], } } ) // プロンプトを登録 mcpServer.registerPrompt( 'code-review', { description: 'コードレビューを依頼するプロンプト', argsSchema: { code: z.string().describe('レビューするコード'), }, }, async ({ code }) => ({ messages: [ { role: 'user', content: { type: 'text', text: `以下のコードをレビューしてください:\n\n${code}`, }, }, ], }) ) // MCPエンドポイント app.all('/mcp', async (c) => { const transport = new StreamableHTTPTransport() await mcpServer.connect(transport) return transport.handleRequest(c) }) // ヘルスチェックエンドポイント app.get('/', (c) => { return c.json({ name: 'Hono MCP Sample Server', version: '1.0.0', status: 'running', endpoints: { mcp: '/mcp', }, }) }) // サーバー起動 const port = 3000 console.log(`サーバーを起動中... ポート: ${port}`) serve({ fetch: app.fetch, port, }) console.log(`✅ サーバーが起動しました: http://localhost:${port}`) console.log(`MCP エンドポイント: http://localhost:${port}/mcp`)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/y-moriya/mcp-sample'

If you have feedback or need assistance with the MCP directory API, please join our Discord server