kensho-kfinance 2.3.0__py3-none-any.whl → 2.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of kensho-kfinance might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: kensho-kfinance
3
- Version: 2.3.0
3
+ Version: 2.4.0
4
4
  Summary: Python CLI for kFinance
5
5
  Author-email: Luke Brown <luke.brown@kensho.com>, Michelle Keoy <michelle.keoy@kensho.com>, Keith Page <keith.page@kensho.com>, Matthew Rosen <matthew.rosen@kensho.com>, Nick Roshdieh <nick.roshdieh@kensho.com>
6
6
  Project-URL: source, https://github.com/kensho-technologies/kfinance
@@ -13,6 +13,8 @@ Description-Content-Type: text/markdown
13
13
  License-File: LICENSE
14
14
  License-File: AUTHORS.md
15
15
  Requires-Dist: cachetools<6,>=5.5
16
+ Requires-Dist: click<=9,>=8.2.1
17
+ Requires-Dist: fastmcp<3,>=2
16
18
  Requires-Dist: langchain-core>=0.3.15
17
19
  Requires-Dist: langchain-google-genai<3,>=2.1.5
18
20
  Requires-Dist: numpy>=1.22.4
@@ -52,7 +54,7 @@ Any questions or suggestions can be sent to the [kFinance Maintainers](kfinance-
52
54
 
53
55
  # Setup
54
56
 
55
- You can install kFinance on [PyPI](https://pypi.org/project/kensho-kfinance/) via
57
+ You can install kFinance on [PyPI](https://pypi.org/project/kensho-kfinance/) via
56
58
 
57
59
  `pip install kensho-kfinance`
58
60
 
@@ -64,7 +66,7 @@ Once access is obtained, get started using the [Authentication Guide](https://do
64
66
 
65
67
  To get started, we provide some notebooks:
66
68
 
67
- - The [LLM-ready API Basic Usage](example_notebooks%2Fbasic_usage.ipynb) notebook demonstrates how
69
+ - The [LLM-ready API Basic Usage](example_notebooks%2Fbasic_usage.ipynb) notebook demonstrates how
68
70
  fetch data with the kFinance client.
69
71
  - The [tool_calling notebooks](example_notebooks%2Ftool_calling) show how the kFinance library can
70
72
  be used for tool calling. We provide notebooks for OpenAI (GPT), Anthropic (Claude), and Google
@@ -73,9 +75,30 @@ wrapper to simplify the integration, and as a lower level non-langchain version.
73
75
 
74
76
  We also provide an [interactive notebook](example_notebooks/basic_usage.ipynb) that demonstrates some usage examples.
75
77
 
78
+ # MCP (Model Context Protocol)
79
+
80
+ To run the kFinance MCP server use:
81
+
82
+ `python -m kfinance.mcp`
83
+
84
+ This function initializes and starts an MCP server that exposes the kFinance tools. The server supports multiple authentication methods and transport protocols to accommodate different deployment scenarios.
85
+
86
+ The server's full signature is as follows:
87
+
88
+ `kfinance.mcp [--stdio,-s]/[--sse, ] --refresh-token <refresh-token> --client-id <client-id> --private-key <private-key>`
89
+
90
+ Authentication Methods (in order of precedence):
91
+ 1. Refresh Token: Uses an existing refresh token for authentication. The `--refresh-token <refresh-token>` argument must be provided.
92
+ 2. Key Pair: Uses client ID and private key for authentication. Both the `--client-id <client-id>` and `--private-key <private-key>` arguments must be provided.
93
+ 3. Browser: Falls back to browser-based authentication flow. This occurs if no auth arguments are provided.
94
+
95
+ Transport Layers:
96
+ - stdio can be set by passing either `--stdio` or `-s`
97
+ - sse can be set by passing `--sse` or no other transport related flag
98
+
76
99
  # Versioning
77
- The kFinance uses semantic versioning (major, minor, patch).
78
- To bump the version, add a new entry in [CHANGELOG.md](kfinance%2FCHANGELOG.md).
100
+ The kFinance uses semantic versioning (major, minor, patch).
101
+ To bump the version, add a new entry in [CHANGELOG.md](kfinance%2FCHANGELOG.md).
79
102
  This will generate a new version of the library as part of the release process.
80
103
 
81
104
  # License
@@ -1,17 +1,18 @@
1
- kensho_kfinance-2.3.0.dist-info/licenses/AUTHORS.md,sha256=0h9ClbI0pu1oKj1M28ROUsaxrbZg-6ukQGl6X4y9noI,68
2
- kensho_kfinance-2.3.0.dist-info/licenses/LICENSE,sha256=bsY4blvSgq6o0FMQ3RXa2NCgco--nHCCchLXzxr6kms,83
3
- kfinance/CHANGELOG.md,sha256=vIFnA_ArXUrV64T76m3KVMX0wGeVMsEi_u4BmQ3os7Y,1418
1
+ kensho_kfinance-2.4.0.dist-info/licenses/AUTHORS.md,sha256=0h9ClbI0pu1oKj1M28ROUsaxrbZg-6ukQGl6X4y9noI,68
2
+ kensho_kfinance-2.4.0.dist-info/licenses/LICENSE,sha256=bsY4blvSgq6o0FMQ3RXa2NCgco--nHCCchLXzxr6kms,83
3
+ kfinance/CHANGELOG.md,sha256=snsLVUBbhNjIau_BsoWKdxxCAPbPHM_rUQ3pNsseF_k,1446
4
4
  kfinance/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
5
  kfinance/batch_request_handling.py,sha256=p6p_G4_BL06GgeKlh7P1k9CUqOMahWCLEw1NoBwbLvU,5698
6
6
  kfinance/constants.py,sha256=UuFzqL253-2tRQfma785K9tfaZGv-o821tO2tVLwc5Q,48813
7
7
  kfinance/fetch.py,sha256=yaCih8PAkOhVHb3tvmBW0x2w4QmXJiyUATu6Yx-xzP4,23851
8
8
  kfinance/kfinance.py,sha256=9lcarUW4fLHJA_U7_1ihCMbNJWs0AztvKf_XEd0qupk,59889
9
+ kfinance/mcp.py,sha256=57OoTrxo5x_v7u1s3WFQNG0LySXyLEGc6CKSVpJLJVw,3717
9
10
  kfinance/meta_classes.py,sha256=3V0nSXDDoake5o7kXnrqXuqNIiwI75KR4IYxFqSPhTE,20736
10
11
  kfinance/prompt.py,sha256=PtVB8c_FcSlVdyGgByAnIFGzuUuBaEjciCqnBJl1hSQ,25133
11
12
  kfinance/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
13
  kfinance/pydantic_models.py,sha256=avpbPqwrAyLqsCbrmFpK_B8_fj1nPlBHrnPxRcBaSkE,774
13
14
  kfinance/server_thread.py,sha256=jUnt1YGoYDkqqz1MbCwd44zJs1T_Z2BCgvj75bdtLgA,2574
14
- kfinance/version.py,sha256=U--yqU7RFo8hQQm8oopUGYLkafj4phNIVfkf5HFEal8,511
15
+ kfinance/version.py,sha256=KyHVhKcn4Ob8_JP09Buz8x5uB4b7RJZLl3FBlZ8sWSY,511
15
16
  kfinance/tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
16
17
  kfinance/tests/conftest.py,sha256=voB-w8P_6L3Nel3rdgylXKe5WWaS1q7nCFt1O04uqoY,948
17
18
  kfinance/tests/test_batch_requests.py,sha256=uXJF2IcRdyBm5SthwIUHMKtkGZ21MY84pg_k1JeSNOY,11430
@@ -19,6 +20,7 @@ kfinance/tests/test_client.py,sha256=O7icZCSDhlQ9WGhzoXlpiSvbuA-mQNJHBYVsilyP_dE
19
20
  kfinance/tests/test_example_notebook.py,sha256=XHwDKw2avyMonTmi3snCcFWNfZhEJOkpBGOZNrMLrhk,6470
20
21
  kfinance/tests/test_fetch.py,sha256=nfnz_ZxE-W3KMzpDaRClX55fQJrRjwLTha-rHTallmE,16713
21
22
  kfinance/tests/test_group_objects.py,sha256=SoMEZmkG4RYdgWOAwxLHHtzIQho92KM01YbQXPUg578,1689
23
+ kfinance/tests/test_mcp.py,sha256=-pmeZ0PDp8GmXWcrWmaNNqMe7OVZONZLrjA0Y4b82os,594
22
24
  kfinance/tests/test_objects.py,sha256=0nDmCFrVcfI8VBo1Ph3YqXNo3uPLsSUiQkjEEHsax1M,30416
23
25
  kfinance/tests/test_tools.py,sha256=EWWWyPlGGES8Cn43_VaDAT7Vdp1nlIETE5dtOrP163o,24447
24
26
  kfinance/tool_calling/README.md,sha256=omJq7Us6r4U45QB7hRpLjRJ5BMalCkZkh4uXBjTbJXc,2022
@@ -43,7 +45,7 @@ kfinance/tool_calling/get_transcript.py,sha256=eB-IsRwD-mllsMOYRZbH35caQ1Y3teKft
43
45
  kfinance/tool_calling/prompts.py,sha256=Yw1DJIMh90cjL-8q6_RMRiSjCtFDXvJAy7QiV5_uAU8,911
44
46
  kfinance/tool_calling/resolve_identifier.py,sha256=npslr6bBCu0qEDV1-8d24F5OC3nQ1KBMphuMbHVC1AU,626
45
47
  kfinance/tool_calling/shared_models.py,sha256=K-NPQyE_7Ew6Cs0zxG1xO2O47gp5uDHdHtWD7wUDZX4,2132
46
- kensho_kfinance-2.3.0.dist-info/METADATA,sha256=3rNOXczOABUGLavlhJUmseCJ0pOL6x00uckfLkJcSkI,4066
47
- kensho_kfinance-2.3.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
48
- kensho_kfinance-2.3.0.dist-info/top_level.txt,sha256=kT_kNwVhfQoOAecY8W7uYah5xaHMoHoAdBIvXh6DaKM,9
49
- kensho_kfinance-2.3.0.dist-info/RECORD,,
48
+ kensho_kfinance-2.4.0.dist-info/METADATA,sha256=yWX6RmluCSz_qoG7EVTJnKJcsOByeZYXmu3M88-ch2A,5191
49
+ kensho_kfinance-2.4.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
50
+ kensho_kfinance-2.4.0.dist-info/top_level.txt,sha256=kT_kNwVhfQoOAecY8W7uYah5xaHMoHoAdBIvXh6DaKM,9
51
+ kensho_kfinance-2.4.0.dist-info/RECORD,,
kfinance/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
 
3
+ ## v2.4.0
4
+ - Add MCP server
5
+
3
6
  ## v2.3.1
4
7
  - Add llm tools for retrieving earnings and transcripts
5
8
 
kfinance/mcp.py ADDED
@@ -0,0 +1,101 @@
1
+ from textwrap import dedent
2
+ from typing import Literal, Optional
3
+
4
+ import click
5
+ from fastmcp import FastMCP
6
+ from fastmcp.utilities.logging import get_logger
7
+
8
+ from kfinance.kfinance import Client
9
+ from kfinance.tool_calling.shared_models import KfinanceTool
10
+
11
+
12
+ logger = get_logger(__name__)
13
+
14
+
15
+ def build_doc_string(tool: KfinanceTool) -> str:
16
+ """Build a formatted documentation string for a Kfinance tool.
17
+
18
+ This function takes a KfinanceTool object and constructs a comprehensive
19
+ documentation string that includes the tool's description and detailed
20
+ information about its arguments, including default values and descriptions.
21
+
22
+ :param tool: The Kfinance tool object containing metadata about the tool's functionality, description, and argument schema.
23
+ :type tool: KfinanceTool
24
+ :return: A formatted documentation string containing for the tool description with detailed argument information.
25
+ :rtype: str
26
+ """
27
+
28
+ description = dedent(f"""
29
+ {tool.description}
30
+
31
+ Args:
32
+ """).strip()
33
+
34
+ for arg_name, arg_field in tool.args_schema.model_fields.items():
35
+ default_value_description = (
36
+ f"Default: {arg_field.default}. " if not arg_field.is_required() else ""
37
+ )
38
+ param_description = f"\n {arg_name}: {default_value_description}{arg_field.description}"
39
+ description += param_description
40
+
41
+ return description
42
+
43
+
44
+ @click.command()
45
+ @click.option("--stdio/--sse", "-s/ ", default=False)
46
+ @click.option("--refresh-token", required=False)
47
+ @click.option("--client-id", required=False)
48
+ @click.option("--private-key", required=False)
49
+ def run_mcp(
50
+ stdio: bool,
51
+ refresh_token: Optional[str] = None,
52
+ client_id: Optional[str] = None,
53
+ private_key: Optional[str] = None,
54
+ ) -> None:
55
+ """Run the Kfinance MCP server with specified configuration.
56
+
57
+ This function initializes and starts an MCP server that exposes Kfinance
58
+ tools. The server supports multiple authentication methods and
59
+ transport protocols to accommodate different deployment scenarios.
60
+
61
+ Authentication Methods (in order of precedence):
62
+ 1. Refresh Token: Uses an existing refresh token for authentication
63
+ 2. Key Pair: Uses client ID and private key for authentication
64
+ 3. Browser: Falls back to browser-based authentication flow
65
+
66
+ :param stdio: If True, use STDIO transport; if False, use SSE transport.
67
+ :type stdio: bool
68
+ :param refresh_token: OAuth refresh token for authentication
69
+ :type refresh_token: str
70
+ :param client_id: Client id for key-pair authentication
71
+ :type client_id: str
72
+ :param private_key: Private key for key-pair authentication.
73
+ :type private_key: str
74
+ """
75
+ transport: Literal["stdio", "sse"] = "stdio" if stdio else "sse"
76
+ logger.info("Sever will run with %s transport", transport)
77
+ if refresh_token:
78
+ logger.info("The client will be authenticated using a refresh token")
79
+ kfinance_client = Client(refresh_token=refresh_token)
80
+ elif client_id and private_key:
81
+ logger.info("The client will be authenticated using a key pair")
82
+ kfinance_client = Client(client_id=client_id, private_key=private_key)
83
+ else:
84
+ logger.info("The client will be authenticated using a browser")
85
+ kfinance_client = Client()
86
+
87
+ kfinance_mcp: FastMCP = FastMCP("Kfinance")
88
+ for tool in kfinance_client.langchain_tools:
89
+ logger.info("Adding %s to server", tool.name)
90
+ kfinance_mcp.tool(
91
+ name_or_fn=getattr(tool, "_run"),
92
+ name=tool.name,
93
+ description=build_doc_string(tool),
94
+ )
95
+
96
+ logger.info("Server starting")
97
+ kfinance_mcp.run(transport=transport)
98
+
99
+
100
+ if __name__ == "__main__":
101
+ run_mcp()
@@ -0,0 +1,16 @@
1
+ from typing import Type
2
+
3
+ import pytest
4
+
5
+ from kfinance.kfinance import Client
6
+ from kfinance.mcp import build_doc_string
7
+ from kfinance.tool_calling import ALL_TOOLS
8
+ from kfinance.tool_calling.shared_models import KfinanceTool
9
+
10
+
11
+ class TestDocStringBuilding:
12
+ @pytest.mark.parametrize("tool_class", ALL_TOOLS)
13
+ def test_build_doc_string(self, mock_client: Client, tool_class: Type[KfinanceTool]):
14
+ """This test build the docstring for each tool. A success is considered if no exception is raised"""
15
+ tool = tool_class(kfinance_client=mock_client)
16
+ build_doc_string(tool)
kfinance/version.py CHANGED
@@ -17,5 +17,5 @@ __version__: str
17
17
  __version_tuple__: VERSION_TUPLE
18
18
  version_tuple: VERSION_TUPLE
19
19
 
20
- __version__ = version = '2.3.0'
21
- __version_tuple__ = version_tuple = (2, 3, 0)
20
+ __version__ = version = '2.4.0'
21
+ __version_tuple__ = version_tuple = (2, 4, 0)