選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

client.py 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #
  2. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from mcp.client.session import ClientSession
  17. from mcp.client.sse import sse_client
  18. async def main():
  19. try:
  20. # To access RAGFlow server in `host` mode, you need to attach `api_key` for each request to indicate identification.
  21. # async with sse_client("http://localhost:9382/sse", headers={"api_key": "ragflow-IyMGI1ZDhjMTA2ZTExZjBiYTMyMGQ4Zm"}) as streams:
  22. async with sse_client("http://localhost:9382/sse") as streams:
  23. async with ClientSession(
  24. streams[0],
  25. streams[1],
  26. ) as session:
  27. await session.initialize()
  28. tools = await session.list_tools()
  29. print(f"{tools.tools=}")
  30. response = await session.call_tool(name="ragflow_retrieval", arguments={"dataset_ids": ["ce3bb17cf27a11efa69751e139332ced"], "document_ids": [], "question": "How to install neovim?"})
  31. print(f"Tool response: {response.model_dump()}")
  32. except Exception as e:
  33. print(e)
  34. if __name__ == "__main__":
  35. from anyio import run
  36. run(main)