🌊
mcp server (Streamable HTTP) に対応したrokadoc変換を試す。
mcp server (Streamable HTTP) のtool
mcp server(Streamable HTTP) でドキュメント変換用のtoolをrokadocが提供したようなので記事化しました。
(APIの使用回数制限あり)
ただ結構mcp client側がSSEは対応してるけど、(Streamable HTTP)に対応していないものもたくさんあるので,今回はPython SDKを使用します。
n8n
Claude アプリ
実は、2025年4月21日現在、Claude アプリではリモートの MCP Server に接続する事が出来ないようです(※)。
rokadocに関して
tool群の取得
✴︎現状 urlは/を最後につける必要がある模様。
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client
async def test_mcp_get_list() -> None:
"""mcpの動作確認を実施. tool群を取得する."""
url = "https://beta-api.rokadoc.ntt.com/v1/app/convert/mcp/"
# Connect to a streamable HTTP server
headers = {"api-key": "ここにAPIキー"}
async with streamablehttp_client(url, headers=headers) as (
read_stream,
write_stream,
_,
):
# Create a session using the client streams
async with ClientSession(read_stream, write_stream) as session:
await session.initialize()
tool_list = await session.list_tools()
print(tool_list)
この非同期関数を実行すると、
二つのツールが取得できます。
Tool(name='file_to_text_with_rokadoc', description='rokadocを用いて、アップロードされたファイルをテキストに変換します.4分のタイムアウトが存在します.', inputSchema={'properties': {'b64_file': {'title': 'B64 File', 'type': 'string'}, 'filename': {'title': 'Filename', 'type': 'string'}}, 'required': ['b64_file', 'filename'], 'title': 'file_to_text_with_rokadocArguments', 'type': 'object'}, annotations=None),
Tool(name='file_to_text_with_rokadoc_per_page', description='rokadocを用いて、アップロードされたファイルをページ単位でテキストに変換します.大きいファイルは、こちらをページごとに呼び出してください.', inputSchema={'properties': {'b64_file': {'title': 'B64 File', 'type': 'string'}, 'filename': {'title': 'Filename', 'type': 'string'}, 'page': {'title': 'Page', 'type': 'string'}}, 'required': ['b64_file', 'filename', 'page'], 'title': 'file_to_text_with_rokadoc_per_pageArguments', 'type': 'object'}, annotations=None)
一つは、ページ単位ごとで、もう一つは、ページ全体に対する処理です。
変換処理を実行する
async def test_mcp_file_to_text_with_rokadoc_per_page() -> None:
"""mcpの動作確認を実施."""
url = "https://beta-api.rokadoc.ntt.com/v1/app/convert/mcp/"
# Connect to a streamable HTTP server
headers = {"api-key": "ここにAPIキー"}
async with streamablehttp_client(url, headers=headers) as (
read_stream,
write_stream,
_,
):
# Create a session using the client streams
async with ClientSession(read_stream, write_stream) as session:
# Initialize the connection
await session.initialize()
with Path.open("sample.pdf", "rb") as f:
file_data = f.read()
# Base64エンコードして文字列化
file_data_b64 = base64.b64encode(file_data).decode("ascii")
result = await session.call_tool("file_to_text_with_rokadoc_per_page", {"b64_file": file_data_b64, "filename": "sample.pdf", "page": "1"})
print("サーバの応答:", result.content[0].text)
print出力の結果
"meta": {
"separate_method": "page"
},
"units": [
{
"unit": 1,
"title": "",
"body": "Welcome to the PDF!\nHere's s............
... "description": "検索対象のファイル名: sample.pdf(page 1)\n====\n\nWelcome to the PDF!\nHere's s
....<td>More data</td>\n </tr>\n <tr>\n <td>2</td>\n <td>Sample data</td>\n <td>More data</td>\n </tr>\n <tr>\n <td>3</td>\n <td>Sample data</td>\n <td>More data</td>\n </tr>\n</table>\n"
}
]
}
pdfドキュメントの変換処理を受け取ることができました。
まだStreamable HTTPに対応しているmcp clientはそこまでありませんが、今後有用できそうですね。
Discussion