Slide 1

Slide 1 text

たった 1 枚の PHP ファイルで実装する MCP サーバ 2025-06-28 PHP Conference Japan 2025

Slide 2

Slide 2 text

MCP、使ってますか?

Slide 3

Slide 3 text

• Model Context Protocol • LLM アプリケーションが外部のシステム等と やりとりをする際のプロトコル • Claude を提供している Anthropic 社が提唱 MCP とは

Slide 4

Slide 4 text

LLM は"尤もらしい続き"を返すことしかできない LLM LLM アプリケー ション 今日の東京の天気は? ユーザーが 「今日の東京の天気は?」 と聞いています。

Slide 5

Slide 5 text

LLM は"尤もらしい続き"を返すことしかできない LLM LLM アプリケー ション 今日の東京の天気は? ユーザーが 「今日の東京の天気は?」 と聞いています。 2025 年の天気は提供できません。 2025 年の天気は提供できません。

Slide 6

Slide 6 text

外界とのやりとりはアプリケーションの役割 LLM LLM アプリケー ション 今日の東京の天気は? ツールとして 天気情報システムが使えます。 ユーザーが 「今日の東京の天気は?」 と聞いています。 天気情報 システム

Slide 7

Slide 7 text

外界とのやりとりはアプリケーションの役割 LLM LLM アプリケー ション 今日の東京の天気は? ツールとして 天気情報システムが使えます。 ユーザーが 「今日の東京の天気は?」 と聞いています。 天気情報システムに 2025 年 6 月 28 日の東京の天気を問い合わせま す。 天気情報 システム 2025-06-28, 東京 晴れ

Slide 8

Slide 8 text

外界とのやりとりはアプリケーションの役割 LLM LLM アプリケー ション 今日の東京の天気は? ツールとして 天気情報システムが使えます。 ユーザーが 「今日の東京の天気は?」 と聞いています。 天気情報システムに 2025 年 6 月 28 日の東京の天気を問い合わせま す。 天気情報 システム 2025-06-28, 東京 晴れ

Slide 9

Slide 9 text

MCP を使わずに連携する場合 各 LLM アプリケーションと MCP の関係 : : Claude Desktop Copilot Chat Gemini CLI

Slide 10

Slide 10 text

MCP で連携する場合 各 LLM アプリケーションと MCP の関係 MCP MCP Client MCP Client MCP Client MCP Server MCP Server : : Claude Desktop Copilot Chat Gemini CLI 1 種類のサーバで複数種のクライアントに対応できる

Slide 11

Slide 11 text

PSR cf.) PHP における FW と PSR の関係 Logger DI Container : 1 種類の実装を複数の FW から利用できる :

Slide 12

Slide 12 text

• 外部のシステム等とやりとりをするために 必ず MCP が必要、というわけではない • あくまで 1 企業が提唱したプロトコルに過ぎない • 連携のために各 LLM アプリケーションが独自で 仕組みを実装したってよい 知っておくとよいこと

Slide 13

Slide 13 text

時は 2025 年 3 月 26 日

Slide 14

Slide 14 text

MCP の新バージョン ※現時点の最新は 2025-06-18

Slide 15

Slide 15 text

MCP サーバを HTTP で実装可能に Streamable HTTP transport https://github.com/modelcontextprotocol/modelcontextprotocol/pull/206

Slide 16

Slide 16 text

PHPer のメインフィールドですね? そう、つまり

Slide 17

Slide 17 text

と、いうわけで

Slide 18

Slide 18 text

実装したものがこちら https://github.com/okashoi/mcp-streamable-http-server-with-vanilla-php

Slide 19

Slide 19 text

Composer 未使用 = Web アプリケーション FW や  MCP SDK などを一切使わない  素 PHP のみ 実装したものがこちら getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; header('Content-Type: application/json'); echo json_encode([ 'jsonrpc' => '2.0', 'id' => $parsedBody['id'], 'result' => $data, ]); exit(); function getInitialInfo(): array { return [ 'protocolVersion' => '2025-03-26', 'capabilities' => [ 'tools' => [ 'listChanged' => false, ], ], 'serverInfo' => [ 'name' => 'date sun info server', 'version' => '1.0.0', ], ]; } function listTools(): array { return [ 'tools' => [ [ 'name' => 'get_sunrise_time', 'description' => '与えらた地点の緯度経度と日付から、その地点の日の出時刻をUTCで取得します。', 'inputSchema' => [ 'type' => 'object', 'properties' => [ 'latitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の北緯。南緯は負の値で表します。', ], 'longitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の東経。西経は負の値で表します。', ], 'date' => [ 'type' => 'string', 'description' => '日の出時刻を知りたい日付。YYYY-MM-DD形式で指定します。デフォルトは今日の日付です。', ], ], 'required' => ['latitude', 'longitude'], ], ], ], ]; } function getSunriseTime(float $latitude, float $longitude, ?string $date = null): array { $timestamp = $date ? strtotime($date) : time(); if ($timestamp === false) { return [ 'content' => [[ 'type' => 'text', 'text' => '日付の形式が不正です。', ]], 'isError' => true, ]; } $sunriseTime = date_sun_info($timestamp, $latitude, $longitude)['sunrise']; if ($sunriseTime === false) { $text = '1日中日が昇りません(極夜)。'; } else if ($sunriseTime === true) { $text = '1日中日が昇ったままです(白夜)。'; } else { $text = '日の出時刻は' . date('H:i', $sunriseTime) . '(UTC)です。'; } return [ 'content' => [[ 'type' => 'text', 'text' => $text, ]], 'isError' => false, ]; } 約 100 行

Slide 20

Slide 20 text

実装の解説

Slide 21

Slide 21 text

せっかく PHP で実装するなら PHP である意味 を持たせたい......

Slide 22

Slide 22 text

PHP が持っている最高に便利な関数 https://www.php.net/manual/ja/function.date-sun-info.php

Slide 23

Slide 23 text

PHP が持っている最高に便利な関数 https://www.php.net/manual/ja/function.date-sun-info.php 今回は日の出時刻の情報を提供する tool を考える

Slide 24

Slide 24 text

MCP サーバ(Streamable HTTP Transport)の仕様 • 受け取るのは /mcp への POST のみ • やりとりするデータは JSON RPC 2.0 に準拠 最小限のMCP サーバを実装するために • 1 つの tool(日の出時刻)だけを提供 • 4 種類の処理を実装するだけで OK • 入力値の検証やエラーハンドリング等は行わない 実装説明

Slide 25

Slide 25 text

MCP サーバ(Streamable HTTP Transport)の仕様 • 受け取るのは /mcp への POST のみ • やりとりするデータは JSON RPC 2.0 に準拠 最小限のMCP サーバを実装するために • 1 つの tool(日の出時刻)だけを提供 • 4 種類の処理を実装するだけで OK • 入力値の検証やエラーハンドリング等は行わない 実装説明

Slide 26

Slide 26 text

getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; header('Content-Type: application/json'); echo json_encode([ 'jsonrpc' => '2.0', 'id' => $parsedBody['id'], 'result' => $data, ]); exit(); function getInitialInfo(): array { return [ 'protocolVersion' => '2025-03-26', 'capabilities' => [ 'tools' => [ 'listChanged' => false, ], ], 'serverInfo' => [ 'name' => 'date sun info server', 'version' => '1.0.0', ], ]; } function listTools(): array { return [ 'tools' => [ [ 'name' => 'get_sunrise_time', 'description' => '与えらた地点の緯度経度と日付から、その地点の日の出時刻をUTCで取得します。', 'inputSchema' => [ 'type' => 'object', 'properties' => [ 'latitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の北緯。南緯は負の値で表します。', ], 'longitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の東経。西経は負の値で表します。', ], 'date' => [ 'type' => 'string', 'description' => '日の出時刻を知りたい日付。YYYY-MM-DD形式で指定します。デフォルトは今日の日付です。', ], ], 'required' => ['latitude', 'longitude'], ], ], ], ]; } function getSunriseTime(float $latitude, float $longitude, ?string $date = null): array { $timestamp = $date ? strtotime($date) : time(); if ($timestamp === false) { return [ 'content' => [[ 'type' => 'text', 'text' => '日付の形式が不正です。', ]], 'isError' => true, ]; } $sunriseTime = date_sun_info($timestamp, $latitude, $longitude)['sunrise']; if ($sunriseTime === false) { $text = '1日中日が昇りません(極夜)。'; } else if ($sunriseTime === true) { $text = '1日中日が昇ったままです(白夜)。'; } else { $text = '日の出時刻は' . date('H:i', $sunriseTime) . '(UTC)です。'; } return [ 'content' => [[ 'type' => 'text', 'text' => $text, ]], 'isError' => false, ]; } 実装説明 - リクエストハンドリング HTTPメソッドの検証などはしない。 なぜなら最小限だから。 ※真似する場合は自己責任で

Slide 27

Slide 27 text

MCP サーバ(Streamable HTTP Transport)の仕様 • 受け取るのは /mcp への POST のみ • やりとりするデータは JSON RPC 2.0 に準拠 最小限のMCP サーバを実装するために • 1 つの tool(日の出時刻)だけを提供 • 4 種類の処理を実装するだけで OK • 入力値の検証やエラーハンドリング等は行わない 実装説明

Slide 28

Slide 28 text

JSON-RPC 2.0 { jsonrpc: "2.0"; id: string | number; method: string; params?: { [key: string]: unknown; }; } { jsonrpc: "2.0"; id: string | number; result?: { [key: string]: unknown; } error?: { code: number; message: string; data?: unknown; } } Request Response Notifications は今回使わないので省略

Slide 29

Slide 29 text

getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; header('Content-Type: application/json'); echo json_encode([ 'jsonrpc' => '2.0', 'id' => $parsedBody['id'], 'result' => $data, ]); exit(); function getInitialInfo(): array { return [ 'protocolVersion' => '2025-03-26', 'capabilities' => [ 'tools' => [ 'listChanged' => false, ], ], 'serverInfo' => [ 'name' => 'date sun info server', 'version' => '1.0.0', ], ]; } function listTools(): array { return [ 'tools' => [ [ 'name' => 'get_sunrise_time', 'description' => '与えらた地点の緯度経度と日付から、その地点の日の出時刻をUTCで取得します。', 'inputSchema' => [ 'type' => 'object', 'properties' => [ 'latitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の北緯。南緯は負の値で表します。', ], 'longitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の東経。西経は負の値で表します。', ], 'date' => [ 'type' => 'string', 'description' => '日の出時刻を知りたい日付。YYYY-MM-DD形式で指定します。デフォルトは今日の日付です。', ], ], 'required' => ['latitude', 'longitude'], ], ], ], ]; } function getSunriseTime(float $latitude, float $longitude, ?string $date = null): array { $timestamp = $date ? strtotime($date) : time(); if ($timestamp === false) { return [ 'content' => [[ 'type' => 'text', 'text' => '日付の形式が不正です。', ]], 'isError' => true, ]; } $sunriseTime = date_sun_info($timestamp, $latitude, $longitude)['sunrise']; if ($sunriseTime === false) { $text = '1日中日が昇りません(極夜)。'; } else if ($sunriseTime === true) { $text = '1日中日が昇ったままです(白夜)。'; } else { $text = '日の出時刻は' . date('H:i', $sunriseTime) . '(UTC)です。'; } return [ 'content' => [[ 'type' => 'text', 'text' => $text, ]], 'isError' => false, ]; } 実装説明 - JSON RPC リクエストのパース $requestBody = file_get_contents('php://input'); $parsedBody = json_decode($requestBody, true); json_decode は必ず成功するものとする。 なぜなら最小限だから。

Slide 30

Slide 30 text

実装説明 - JSON RPC レスポンス getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; header('Content-Type: application/json'); echo json_encode([ 'jsonrpc' => '2.0', 'id' => $parsedBody['id'], 'result' => $data, ]); exit(); function getInitialInfo(): array { return [ 'protocolVersion' => '2025-03-26', 'capabilities' => [ 'tools' => [ 'listChanged' => false, ], ], 'serverInfo' => [ 'name' => 'date sun info server', 'version' => '1.0.0', ], ]; } function listTools(): array { return [ 'tools' => [ [ 'name' => 'get_sunrise_time', 'description' => '与えらた地点の緯度経度と日付から、その地点の日の出時刻をUTCで取得します。', 'inputSchema' => [ 'type' => 'object', 'properties' => [ 'latitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の北緯。南緯は負の値で表します。', ], 'longitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の東経。西経は負の値で表します。', ], 'date' => [ 'type' => 'string', 'description' => '日の出時刻を知りたい日付。YYYY-MM-DD形式で指定します。デフォルトは今日の日付です。', ], ], 'required' => ['latitude', 'longitude'], ], ], ], ]; } function getSunriseTime(float $latitude, float $longitude, ?string $date = null): array { $timestamp = $date ? strtotime($date) : time(); if ($timestamp === false) { return [ 'content' => [[ 'type' => 'text', 'text' => '日付の形式が不正です。', ]], 'isError' => true, ]; } $sunriseTime = date_sun_info($timestamp, $latitude, $longitude)['sunrise']; if ($sunriseTime === false) { $text = '1日中日が昇りません(極夜)。'; } else if ($sunriseTime === true) { $text = '1日中日が昇ったままです(白夜)。'; } else { $text = '日の出時刻は' . date('H:i', $sunriseTime) . '(UTC)です。'; } return [ 'content' => [[ 'type' => 'text', 'text' => $text, ]], 'isError' => false, ]; } header('Content-Type: application/json'); echo json_encode([ 'jsonrpc' => '2.0', 'id' => $parsedBody['id'], 'result' => $data, ]); exit(); $parsedBody には必ず id が存在するものとする。 なぜなら最小限だから。

Slide 31

Slide 31 text

MCP サーバ(Streamable HTTP Transport)の仕様 • 受け取るのは /mcp への POST のみ • やりとりするデータは JSON RPC 2.0 に準拠 最小限のMCP サーバを実装するために • 1 つの tool(日の出時刻)だけを提供 • 4 種類の処理を実装するだけで OK • 入力値の検証やエラーハンドリング等は行わない 実装説明

Slide 32

Slide 32 text

実装説明 getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; header('Content-Type: application/json'); echo json_encode([ 'jsonrpc' => '2.0', 'id' => $parsedBody['id'], 'result' => $data, ]); exit(); function getInitialInfo(): array { return [ 'protocolVersion' => '2025-03-26', 'capabilities' => [ 'tools' => [ 'listChanged' => false, ], ], 'serverInfo' => [ 'name' => 'date sun info server', 'version' => '1.0.0', ], ]; } function listTools(): array { return [ 'tools' => [ [ 'name' => 'get_sunrise_time', 'description' => '与えらた地点の緯度経度と日付から、その地点の日の出時刻をUTCで取得します。', 'inputSchema' => [ 'type' => 'object', 'properties' => [ 'latitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の北緯。南緯は負の値で表します。', ], 'longitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の東経。西経は負の値で表します。', ], 'date' => [ 'type' => 'string', 'description' => '日の出時刻を知りたい日付。YYYY-MM-DD形式で指定します。デフォルトは今日の日付です。', ], ], 'required' => ['latitude', 'longitude'], ], ], ], ]; } function getSunriseTime(float $latitude, float $longitude, ?string $date = null): array { $timestamp = $date ? strtotime($date) : time(); if ($timestamp === false) { return [ 'content' => [[ 'type' => 'text', 'text' => '日付の形式が不正です。', ]], 'isError' => true, ]; } $sunriseTime = date_sun_info($timestamp, $latitude, $longitude)['sunrise']; if ($sunriseTime === false) { $text = '1日中日が昇りません(極夜)。'; } else if ($sunriseTime === true) { $text = '1日中日が昇ったままです(白夜)。'; } else { $text = '日の出時刻は' . date('H:i', $sunriseTime) . '(UTC)です。'; } return [ 'content' => [[ 'type' => 'text', 'text' => $text, ]], 'isError' => false, ]; } $data = match ($parsedBody['method']) { 'initialize' => getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; $parsedBody は常に正しい形式であるとする。 なぜなら最小限(ry

Slide 33

Slide 33 text

実装説明 getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; header('Content-Type: application/json'); echo json_encode([ 'jsonrpc' => '2.0', 'id' => $parsedBody['id'], 'result' => $data, ]); exit(); function getInitialInfo(): array { return [ 'protocolVersion' => '2025-03-26', 'capabilities' => [ 'tools' => [ 'listChanged' => false, ], ], 'serverInfo' => [ 'name' => 'date sun info server', 'version' => '1.0.0', ], ]; } function listTools(): array { return [ 'tools' => [ [ 'name' => 'get_sunrise_time', 'description' => '与えらた地点の緯度経度と日付から、その地点の日の出時刻をUTCで取得します。', 'inputSchema' => [ 'type' => 'object', 'properties' => [ 'latitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の北緯。南緯は負の値で表します。', ], 'longitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の東経。西経は負の値で表します。', ], 'date' => [ 'type' => 'string', 'description' => '日の出時刻を知りたい日付。YYYY-MM-DD形式で指定します。デフォルトは今日の日付です。', ], ], 'required' => ['latitude', 'longitude'], ], ], ], ]; } function getSunriseTime(float $latitude, float $longitude, ?string $date = null): array { $timestamp = $date ? strtotime($date) : time(); if ($timestamp === false) { return [ 'content' => [[ 'type' => 'text', 'text' => '日付の形式が不正です。', ]], 'isError' => true, ]; } $sunriseTime = date_sun_info($timestamp, $latitude, $longitude)['sunrise']; if ($sunriseTime === false) { $text = '1日中日が昇りません(極夜)。'; } else if ($sunriseTime === true) { $text = '1日中日が昇ったままです(白夜)。'; } else { $text = '日の出時刻は' . date('H:i', $sunriseTime) . '(UTC)です。'; } return [ 'content' => [[ 'type' => 'text', 'text' => $text, ]], 'isError' => false, ]; } $data = match ($parsedBody['method']) { 'initialize' => getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; $parsedBody は常に正しい形式であるとする。 なぜなら最小限(ry 固定値を返すだけ

Slide 34

Slide 34 text

実装説明 - initialize getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; header('Content-Type: application/json'); echo json_encode([ 'jsonrpc' => '2.0', 'id' => $parsedBody['id'], 'result' => $data, ]); exit(); function getInitialInfo(): array { return [ 'protocolVersion' => '2025-03-26', 'capabilities' => [ 'tools' => [ 'listChanged' => false, ], ], 'serverInfo' => [ 'name' => 'date sun info server', 'version' => '1.0.0', ], ]; } function listTools(): array { return [ 'tools' => [ [ 'name' => 'get_sunrise_time', 'description' => '与えらた地点の緯度経度と日付から、その地点の日の出時刻をUTCで取得します。', 'inputSchema' => [ 'type' => 'object', 'properties' => [ 'latitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の北緯。南緯は負の値で表します。', ], 'longitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の東経。西経は負の値で表します。', ], 'date' => [ 'type' => 'string', 'description' => '日の出時刻を知りたい日付。YYYY-MM-DD形式で指定します。デフォルトは今日の日付です。', ], ], 'required' => ['latitude', 'longitude'], ], ], ], ]; } function getSunriseTime(float $latitude, float $longitude, ?string $date = null): array { $timestamp = $date ? strtotime($date) : time(); if ($timestamp === false) { return [ 'content' => [[ 'type' => 'text', 'text' => '日付の形式が不正です。', ]], 'isError' => true, ]; } $sunriseTime = date_sun_info($timestamp, $latitude, $longitude)['sunrise']; if ($sunriseTime === false) { $text = '1日中日が昇りません(極夜)。'; } else if ($sunriseTime === true) { $text = '1日中日が昇ったままです(白夜)。'; } else { $text = '日の出時刻は' . date('H:i', $sunriseTime) . '(UTC)です。'; } return [ 'content' => [[ 'type' => 'text', 'text' => $text, ]], 'isError' => false, ]; } $data = match ($parsedBody['method']) { 'initialize' => getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; $parsedBody は常に正しい形式であるとする。 なぜなら最小限(ry

Slide 35

Slide 35 text

実装説明 - initialize function getInitialInfo(): array { return [ 'protocolVersion' => '2025-03-26', 'capabilities' => [ 'tools' => [ 'listChanged' => false, ], ], 'serverInfo' => [ 'name' => 'date sun info server', 'version' => '1.0.0', ], ]; } getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; header('Content-Type: application/json'); echo json_encode([ 'jsonrpc' => '2.0', 'id' => $parsedBody['id'], 'result' => $data, ]); exit(); function getInitialInfo(): array { return [ 'protocolVersion' => '2025-03-26', 'capabilities' => [ 'tools' => [ 'listChanged' => false, ], ], 'serverInfo' => [ 'name' => 'date sun info server', 'version' => '1.0.0', ], ]; } function listTools(): array { return [ 'tools' => [ [ 'name' => 'get_sunrise_time', 'description' => '与えらた地点の緯度経度と日付から、その地点の日の出時刻をUTCで取得します。', 'inputSchema' => [ 'type' => 'object', 'properties' => [ 'latitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の北緯。南緯は負の値で表します。', ], 'longitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の東経。西経は負の値で表します。', ], 'date' => [ 'type' => 'string', 'description' => '日の出時刻を知りたい日付。YYYY-MM-DD形式で指定します。デフォルトは今日の日付です。', ], ], 'required' => ['latitude', 'longitude'], ], ], ], ]; } function getSunriseTime(float $latitude, float $longitude, ?string $date = null): array { $timestamp = $date ? strtotime($date) : time(); if ($timestamp === false) { return [ 'content' => [[ 'type' => 'text', 'text' => '日付の形式が不正です。', ]], 'isError' => true, ]; } $sunriseTime = date_sun_info($timestamp, $latitude, $longitude)['sunrise']; if ($sunriseTime === false) { $text = '1日中日が昇りません(極夜)。'; } else if ($sunriseTime === true) { $text = '1日中日が昇ったままです(白夜)。'; } else { $text = '日の出時刻は' . date('H:i', $sunriseTime) . '(UTC)です。'; } return [ 'content' => [[ 'type' => 'text', 'text' => $text, ]], 'isError' => false, ]; }

Slide 36

Slide 36 text

実装説明 - initialize function getInitialInfo(): array { return [ 'protocolVersion' => '2025-03-26', 'capabilities' => [ 'tools' => [ 'listChanged' => false, ], ], 'serverInfo' => [ 'name' => 'date sun info server', 'version' => '1.0.0', ], ]; } getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; header('Content-Type: application/json'); echo json_encode([ 'jsonrpc' => '2.0', 'id' => $parsedBody['id'], 'result' => $data, ]); exit(); function getInitialInfo(): array { return [ 'protocolVersion' => '2025-03-26', 'capabilities' => [ 'tools' => [ 'listChanged' => false, ], ], 'serverInfo' => [ 'name' => 'date sun info server', 'version' => '1.0.0', ], ]; } function listTools(): array { return [ 'tools' => [ [ 'name' => 'get_sunrise_time', 'description' => '与えらた地点の緯度経度と日付から、その地点の日の出時刻をUTCで取得します。', 'inputSchema' => [ 'type' => 'object', 'properties' => [ 'latitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の北緯。南緯は負の値で表します。', ], 'longitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の東経。西経は負の値で表します。', ], 'date' => [ 'type' => 'string', 'description' => '日の出時刻を知りたい日付。YYYY-MM-DD形式で指定します。デフォルトは今日の日付です。', ], ], 'required' => ['latitude', 'longitude'], ], ], ], ]; } function getSunriseTime(float $latitude, float $longitude, ?string $date = null): array { $timestamp = $date ? strtotime($date) : time(); if ($timestamp === false) { return [ 'content' => [[ 'type' => 'text', 'text' => '日付の形式が不正です。', ]], 'isError' => true, ]; } $sunriseTime = date_sun_info($timestamp, $latitude, $longitude)['sunrise']; if ($sunriseTime === false) { $text = '1日中日が昇りません(極夜)。'; } else if ($sunriseTime === true) { $text = '1日中日が昇ったままです(白夜)。'; } else { $text = '日の出時刻は' . date('H:i', $sunriseTime) . '(UTC)です。'; } return [ 'content' => [[ 'type' => 'text', 'text' => $text, ]], 'isError' => false, ]; } tool のみを提供

Slide 37

Slide 37 text

実装説明 - initialize function getInitialInfo(): array { return [ 'protocolVersion' => '2025-03-26', 'capabilities' => [ 'tools' => [ 'listChanged' => false, ], ], 'serverInfo' => [ 'name' => 'date sun info server', 'version' => '1.0.0', ], ]; } getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; header('Content-Type: application/json'); echo json_encode([ 'jsonrpc' => '2.0', 'id' => $parsedBody['id'], 'result' => $data, ]); exit(); function getInitialInfo(): array { return [ 'protocolVersion' => '2025-03-26', 'capabilities' => [ 'tools' => [ 'listChanged' => false, ], ], 'serverInfo' => [ 'name' => 'date sun info server', 'version' => '1.0.0', ], ]; } function listTools(): array { return [ 'tools' => [ [ 'name' => 'get_sunrise_time', 'description' => '与えらた地点の緯度経度と日付から、その地点の日の出時刻をUTCで取得します。', 'inputSchema' => [ 'type' => 'object', 'properties' => [ 'latitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の北緯。南緯は負の値で表します。', ], 'longitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の東経。西経は負の値で表します。', ], 'date' => [ 'type' => 'string', 'description' => '日の出時刻を知りたい日付。YYYY-MM-DD形式で指定します。デフォルトは今日の日付です。', ], ], 'required' => ['latitude', 'longitude'], ], ], ], ]; } function getSunriseTime(float $latitude, float $longitude, ?string $date = null): array { $timestamp = $date ? strtotime($date) : time(); if ($timestamp === false) { return [ 'content' => [[ 'type' => 'text', 'text' => '日付の形式が不正です。', ]], 'isError' => true, ]; } $sunriseTime = date_sun_info($timestamp, $latitude, $longitude)['sunrise']; if ($sunriseTime === false) { $text = '1日中日が昇りません(極夜)。'; } else if ($sunriseTime === true) { $text = '1日中日が昇ったままです(白夜)。'; } else { $text = '日の出時刻は' . date('H:i', $sunriseTime) . '(UTC)です。'; } return [ 'content' => [[ 'type' => 'text', 'text' => $text, ]], 'isError' => false, ]; } 変更を通知しない

Slide 38

Slide 38 text

実装説明 - notifications/initialized getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; header('Content-Type: application/json'); echo json_encode([ 'jsonrpc' => '2.0', 'id' => $parsedBody['id'], 'result' => $data, ]); exit(); function getInitialInfo(): array { return [ 'protocolVersion' => '2025-03-26', 'capabilities' => [ 'tools' => [ 'listChanged' => false, ], ], 'serverInfo' => [ 'name' => 'date sun info server', 'version' => '1.0.0', ], ]; } function listTools(): array { return [ 'tools' => [ [ 'name' => 'get_sunrise_time', 'description' => '与えらた地点の緯度経度と日付から、その地点の日の出時刻をUTCで取得します。', 'inputSchema' => [ 'type' => 'object', 'properties' => [ 'latitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の北緯。南緯は負の値で表します。', ], 'longitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の東経。西経は負の値で表します。', ], 'date' => [ 'type' => 'string', 'description' => '日の出時刻を知りたい日付。YYYY-MM-DD形式で指定します。デフォルトは今日の日付です。', ], ], 'required' => ['latitude', 'longitude'], ], ], ], ]; } function getSunriseTime(float $latitude, float $longitude, ?string $date = null): array { $timestamp = $date ? strtotime($date) : time(); if ($timestamp === false) { return [ 'content' => [[ 'type' => 'text', 'text' => '日付の形式が不正です。', ]], 'isError' => true, ]; } $sunriseTime = date_sun_info($timestamp, $latitude, $longitude)['sunrise']; if ($sunriseTime === false) { $text = '1日中日が昇りません(極夜)。'; } else if ($sunriseTime === true) { $text = '1日中日が昇ったままです(白夜)。'; } else { $text = '日の出時刻は' . date('H:i', $sunriseTime) . '(UTC)です。'; } return [ 'content' => [[ 'type' => 'text', 'text' => $text, ]], 'isError' => false, ]; } $data = match ($parsedBody['method']) { 'initialize' => getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; $parsedBody は常に正しい形式であるとする。 なぜなら最小限(ry クライアントの初期化が成功したら送られてくる

Slide 39

Slide 39 text

実装説明 - tools/list getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; header('Content-Type: application/json'); echo json_encode([ 'jsonrpc' => '2.0', 'id' => $parsedBody['id'], 'result' => $data, ]); exit(); function getInitialInfo(): array { return [ 'protocolVersion' => '2025-03-26', 'capabilities' => [ 'tools' => [ 'listChanged' => false, ], ], 'serverInfo' => [ 'name' => 'date sun info server', 'version' => '1.0.0', ], ]; } function listTools(): array { return [ 'tools' => [ [ 'name' => 'get_sunrise_time', 'description' => '与えらた地点の緯度経度と日付から、その地点の日の出時刻をUTCで取得します。', 'inputSchema' => [ 'type' => 'object', 'properties' => [ 'latitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の北緯。南緯は負の値で表します。', ], 'longitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の東経。西経は負の値で表します。', ], 'date' => [ 'type' => 'string', 'description' => '日の出時刻を知りたい日付。YYYY-MM-DD形式で指定します。デフォルトは今日の日付です。', ], ], 'required' => ['latitude', 'longitude'], ], ], ], ]; } function getSunriseTime(float $latitude, float $longitude, ?string $date = null): array { $timestamp = $date ? strtotime($date) : time(); if ($timestamp === false) { return [ 'content' => [[ 'type' => 'text', 'text' => '日付の形式が不正です。', ]], 'isError' => true, ]; } $sunriseTime = date_sun_info($timestamp, $latitude, $longitude)['sunrise']; if ($sunriseTime === false) { $text = '1日中日が昇りません(極夜)。'; } else if ($sunriseTime === true) { $text = '1日中日が昇ったままです(白夜)。'; } else { $text = '日の出時刻は' . date('H:i', $sunriseTime) . '(UTC)です。'; } return [ 'content' => [[ 'type' => 'text', 'text' => $text, ]], 'isError' => false, ]; } $data = match ($parsedBody['method']) { 'initialize' => getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; $parsedBody は常に正しい形式であるとする。 なぜなら最小限(ry 利用可能なツールの情報を提供

Slide 40

Slide 40 text

function listTools(): array { return [ 'tools' => [ [ 'name' => 'get_sunrise_time', 'description' => '与えらた地点の緯度経度と日付から、その地点の日の出時刻をUTCで取得します。', 'inputSchema' => [ 'type' => 'object', 'properties' => [ 'latitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の北緯。南緯は負の値で表します。', ], // (中略) ], 'required' => ['latitude', 'longitude'], ], ], ], ]; } 実装説明 - tools/list

Slide 41

Slide 41 text

function listTools(): array { return [ 'tools' => [ [ 'name' => 'get_sunrise_time', 'description' => '与えらた地点の緯度経度と日付から、その地点の日の出時刻をUTCで取得します。', 'inputSchema' => [ 'type' => 'object', 'properties' => [ 'latitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の北緯。南緯は負の値で表します。', ], // (中略) ], 'required' => ['latitude', 'longitude'], ], ], ], ]; } 実装説明 - tools/list

Slide 42

Slide 42 text

実装説明 - tools/call getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; header('Content-Type: application/json'); echo json_encode([ 'jsonrpc' => '2.0', 'id' => $parsedBody['id'], 'result' => $data, ]); exit(); function getInitialInfo(): array { return [ 'protocolVersion' => '2025-03-26', 'capabilities' => [ 'tools' => [ 'listChanged' => false, ], ], 'serverInfo' => [ 'name' => 'date sun info server', 'version' => '1.0.0', ], ]; } function listTools(): array { return [ 'tools' => [ [ 'name' => 'get_sunrise_time', 'description' => '与えらた地点の緯度経度と日付から、その地点の日の出時刻をUTCで取得します。', 'inputSchema' => [ 'type' => 'object', 'properties' => [ 'latitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の北緯。南緯は負の値で表します。', ], 'longitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の東経。西経は負の値で表します。', ], 'date' => [ 'type' => 'string', 'description' => '日の出時刻を知りたい日付。YYYY-MM-DD形式で指定します。デフォルトは今日の日付です。', ], ], 'required' => ['latitude', 'longitude'], ], ], ], ]; } function getSunriseTime(float $latitude, float $longitude, ?string $date = null): array { $timestamp = $date ? strtotime($date) : time(); if ($timestamp === false) { return [ 'content' => [[ 'type' => 'text', 'text' => '日付の形式が不正です。', ]], 'isError' => true, ]; } $sunriseTime = date_sun_info($timestamp, $latitude, $longitude)['sunrise']; if ($sunriseTime === false) { $text = '1日中日が昇りません(極夜)。'; } else if ($sunriseTime === true) { $text = '1日中日が昇ったままです(白夜)。'; } else { $text = '日の出時刻は' . date('H:i', $sunriseTime) . '(UTC)です。'; } return [ 'content' => [[ 'type' => 'text', 'text' => $text, ]], 'isError' => false, ]; } $data = match ($parsedBody['method']) { 'initialize' => getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; $parsedBody は常に正しい形式であるとする。 なぜなら最小限(ry

Slide 43

Slide 43 text

実装説明 - tools/call getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; header('Content-Type: application/json'); echo json_encode([ 'jsonrpc' => '2.0', 'id' => $parsedBody['id'], 'result' => $data, ]); exit(); function getInitialInfo(): array { return [ 'protocolVersion' => '2025-03-26', 'capabilities' => [ 'tools' => [ 'listChanged' => false, ], ], 'serverInfo' => [ 'name' => 'date sun info server', 'version' => '1.0.0', ], ]; } function listTools(): array { return [ 'tools' => [ [ 'name' => 'get_sunrise_time', 'description' => '与えらた地点の緯度経度と日付から、その地点の日の出時刻をUTCで取得します。', 'inputSchema' => [ 'type' => 'object', 'properties' => [ 'latitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の北緯。南緯は負の値で表します。', ], 'longitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の東経。西経は負の値で表します。', ], 'date' => [ 'type' => 'string', 'description' => '日の出時刻を知りたい日付。YYYY-MM-DD形式で指定します。デフォルトは今日の日付です。', ], ], 'required' => ['latitude', 'longitude'], ], ], ], ]; } function getSunriseTime(float $latitude, float $longitude, ?string $date = null): array { $timestamp = $date ? strtotime($date) : time(); if ($timestamp === false) { return [ 'content' => [[ 'type' => 'text', 'text' => '日付の形式が不正です。', ]], 'isError' => true, ]; } $sunriseTime = date_sun_info($timestamp, $latitude, $longitude)['sunrise']; if ($sunriseTime === false) { $text = '1日中日が昇りません(極夜)。'; } else if ($sunriseTime === true) { $text = '1日中日が昇ったままです(白夜)。'; } else { $text = '日の出時刻は' . date('H:i', $sunriseTime) . '(UTC)です。'; } return [ 'content' => [[ 'type' => 'text', 'text' => $text, ]], 'isError' => false, ]; } $data = match ($parsedBody['method']) { 'initialize' => getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; $parsedBody は常に正しい形式であるとする。 なぜなら最小限(ry 今回 tool は 1種類だけ なので決め打ち

Slide 44

Slide 44 text

実装説明 - tools/call - get_sunrise_time getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; header('Content-Type: application/json'); echo json_encode([ 'jsonrpc' => '2.0', 'id' => $parsedBody['id'], 'result' => $data, ]); exit(); function getInitialInfo(): array { return [ 'protocolVersion' => '2025-03-26', 'capabilities' => [ 'tools' => [ 'listChanged' => false, ], ], 'serverInfo' => [ 'name' => 'date sun info server', 'version' => '1.0.0', ], ]; } function listTools(): array { return [ 'tools' => [ [ 'name' => 'get_sunrise_time', 'description' => '与えらた地点の緯度経度と日付から、その地点の日の出時刻をUTCで取得します。', 'inputSchema' => [ 'type' => 'object', 'properties' => [ 'latitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の北緯。南緯は負の値で表します。', ], 'longitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の東経。西経は負の値で表します。', ], 'date' => [ 'type' => 'string', 'description' => '日の出時刻を知りたい日付。YYYY-MM-DD形式で指定します。デフォルトは今日の日付です。', ], ], 'required' => ['latitude', 'longitude'], ], ], ], ]; } function getSunriseTime(float $latitude, float $longitude, ?string $date = null): array { $timestamp = $date ? strtotime($date) : time(); if ($timestamp === false) { return [ 'content' => [[ 'type' => 'text', 'text' => '日付の形式が不正です。', ]], 'isError' => true, ]; } $sunriseTime = date_sun_info($timestamp, $latitude, $longitude)['sunrise']; if ($sunriseTime === false) { $text = '1日中日が昇りません(極夜)。'; } else if ($sunriseTime === true) { $text = '1日中日が昇ったままです(白夜)。'; } else { $text = '日の出時刻は' . date('H:i', $sunriseTime) . '(UTC)です。'; } return [ 'content' => [[ 'type' => 'text', 'text' => $text, ]], 'isError' => false, ]; } $timestamp = $date ? strtotime($date) : time(); if ($timestamp === false) { return [ 'content' => [[ 'type' => 'text', 'text' => '日付の形式が不正です。 ', ]], 'isError' => true, ]; }

Slide 45

Slide 45 text

実装説明 - tools/call - get_sunrise_time getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; header('Content-Type: application/json'); echo json_encode([ 'jsonrpc' => '2.0', 'id' => $parsedBody['id'], 'result' => $data, ]); exit(); function getInitialInfo(): array { return [ 'protocolVersion' => '2025-03-26', 'capabilities' => [ 'tools' => [ 'listChanged' => false, ], ], 'serverInfo' => [ 'name' => 'date sun info server', 'version' => '1.0.0', ], ]; } function listTools(): array { return [ 'tools' => [ [ 'name' => 'get_sunrise_time', 'description' => '与えらた地点の緯度経度と日付から、その地点の日の出時刻をUTCで取得します。', 'inputSchema' => [ 'type' => 'object', 'properties' => [ 'latitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の北緯。南緯は負の値で表します。', ], 'longitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の東経。西経は負の値で表します。', ], 'date' => [ 'type' => 'string', 'description' => '日の出時刻を知りたい日付。YYYY-MM-DD形式で指定します。デフォルトは今日の日付です。', ], ], 'required' => ['latitude', 'longitude'], ], ], ], ]; } function getSunriseTime(float $latitude, float $longitude, ?string $date = null): array { $timestamp = $date ? strtotime($date) : time(); if ($timestamp === false) { return [ 'content' => [[ 'type' => 'text', 'text' => '日付の形式が不正です。', ]], 'isError' => true, ]; } $sunriseTime = date_sun_info($timestamp, $latitude, $longitude)['sunrise']; if ($sunriseTime === false) { $text = '1日中日が昇りません(極夜)。'; } else if ($sunriseTime === true) { $text = '1日中日が昇ったままです(白夜)。'; } else { $text = '日の出時刻は' . date('H:i', $sunriseTime) . '(UTC)です。'; } return [ 'content' => [[ 'type' => 'text', 'text' => $text, ]], 'isError' => false, ]; } $sunriseTime = date_sun_info($timestamp, $latitude, $longitude)['sunrise']; if ($sunriseTime === false) { $text = '1日中日が昇りません(極夜)。 '; } else if ($sunriseTime === true) { $text = '1日中日が昇ったままです(白夜)。 '; } else { $text = '日の出時刻は' . date('H:i', $sunriseTime) . '(UTC)です。'; } return [ 'content' => [[ 'type' => 'text', 'text' => $text, ]], 'isError' => false, ];

Slide 46

Slide 46 text

Composer 未使用 = Web アプリケーション FW や  MCP SDK などを一切使わない  素 PHP のみ 実装説明、以上 getInitialInfo(), 'notifications/initialized' => http_response_code(202) && exit(), 'tools/list' => listTools(), 'tools/call' => getSunriseTime( (float)$parsedBody['params']['arguments']['latitude'], (float)$parsedBody['params']['arguments']['longitude'], $parsedBody['params']['arguments']['date'] ?? null, ), }; header('Content-Type: application/json'); echo json_encode([ 'jsonrpc' => '2.0', 'id' => $parsedBody['id'], 'result' => $data, ]); exit(); function getInitialInfo(): array { return [ 'protocolVersion' => '2025-03-26', 'capabilities' => [ 'tools' => [ 'listChanged' => false, ], ], 'serverInfo' => [ 'name' => 'date sun info server', 'version' => '1.0.0', ], ]; } function listTools(): array { return [ 'tools' => [ [ 'name' => 'get_sunrise_time', 'description' => '与えらた地点の緯度経度と日付から、その地点の日の出時刻をUTCで取得します。', 'inputSchema' => [ 'type' => 'object', 'properties' => [ 'latitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の北緯。南緯は負の値で表します。', ], 'longitude' => [ 'type' => 'number', 'description' => '日の出時刻を知りたい地点の東経。西経は負の値で表します。', ], 'date' => [ 'type' => 'string', 'description' => '日の出時刻を知りたい日付。YYYY-MM-DD形式で指定します。デフォルトは今日の日付です。', ], ], 'required' => ['latitude', 'longitude'], ], ], ], ]; } function getSunriseTime(float $latitude, float $longitude, ?string $date = null): array { $timestamp = $date ? strtotime($date) : time(); if ($timestamp === false) { return [ 'content' => [[ 'type' => 'text', 'text' => '日付の形式が不正です。', ]], 'isError' => true, ]; } $sunriseTime = date_sun_info($timestamp, $latitude, $longitude)['sunrise']; if ($sunriseTime === false) { $text = '1日中日が昇りません(極夜)。'; } else if ($sunriseTime === true) { $text = '1日中日が昇ったままです(白夜)。'; } else { $text = '日の出時刻は' . date('H:i', $sunriseTime) . '(UTC)です。'; } return [ 'content' => [[ 'type' => 'text', 'text' => $text, ]], 'isError' => false, ]; } 約 100 行

Slide 47

Slide 47 text

実際に使ってみる (GitHub Copilot Chat / VSCode)

Slide 48

Slide 48 text

MCP サーバの起動

Slide 49

Slide 49 text

MCP サーバの登録 { "servers": { "date-sun-info": { "url": "http://localhost/mcp" } } } .vscode/mcp.json

Slide 50

Slide 50 text

MCP サーバの登録

Slide 51

Slide 51 text

実際に使ってみる

Slide 52

Slide 52 text

実際に使ってみる

Slide 53

Slide 53 text

実際に使ってみる

Slide 54

Slide 54 text

実際に使ってみる

Slide 55

Slide 55 text

実際に使ってみる

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

詳細な仕様は公式のドキュメントをチェック https://modelcontextprotocol.io/specification

Slide 58

Slide 58 text

詳細な仕様は公式のドキュメントをチェック https://github.com/modelcontextprotocol/modelcontextprotocol/tree/main/schema

Slide 59

Slide 59 text

ちなみに

Slide 60

Slide 60 text

MCP サーバなしで同じ質問をした場合

Slide 61

Slide 61 text

MCP サーバなしで同じ質問をした場合

Slide 62

Slide 62 text

MCP サーバなしで同じ質問をした場合 実際には「それを MCP サーバにする必要があるか?」 をちゃんと考えましょう

Slide 63

Slide 63 text

所属:株式会社リンケージ 登壇: 寄稿: 岡田 正平/おかしょい X: @okashoi GitHub: @okashoi